I'm learning basics of Haskell and trying to solve project Euler's simple tasks: find largest palindrome of 3 digit numbers (100.999). I wrote this code:
palindrome = maximum $ filter (\a -> reverse a == a) $
map show [ x*y :: Int | x <- [100 .. 999],
y <- [x, x+1 .. 999]]
It gives a wrong answer = 99999, when I change it to x <- [307 .. 999]answer still is wrong: 94249 (all palindromes, but not the largest) and finally when I change it to x <- [308 .. 999], it gives me right answer: 906609.
I really don't understand this behaviour: it seems there is some kind of overflow and truncation happens inside the generated list. Can someone explain me this wrong behaviour? I don't want you to answer the task: i know my solution is not very efficient. I just want you to explain this code behaviour (list truncation or memory issues). Thanks.
The result of
filteris a list ofStringvalues, somaximumis comparing them lexicographically. You need to convert the values back toInts first. The type signature ensures thatreadreturns values of the correct type.Another approach is to only convert a value to a
Stringin the filter itself: