English 中文(简体)
Euler Problem in Haskell -- Can Someone Spot My Error
原标题:
  • 时间:2009-11-18 20:02:13
  •  标签:
  • haskell

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up to the task, but I m getting a result that looks inconsistent to say the least.

Here s my palindrome detector (which was simplicity itself to code):

isPalindrome :: String -> Bool
isPalindrome [] = True
isPalindrome str = let str2 = reverse str
                   in (str2 == str)

From here it s a simple question of writing a function to detect when a product forms a palindrome (and possibly to subtract one from one of the multiplicands and recurse over a brute-force search if it doesn t). Here s my very simplified version of this, stripped down and returning an IO action for debugging:

findPal :: Integer -> Integer -> IO()
findPal 1 y = putStrLn "reached 1"
findPal x y = let pal = isPalindrome $ show mult 
                  mult = x * y
                  in case pal of
                          true -> putStrLn $ "mult is " ++ (show mult)
                          false -> putStrLn "pal is false"

Here are two separate outputs in GHCi:

*Main> isPalindrome $ show (999*999)
False
*Main> findPal 999 999
mult is 998001

In other words, the call to isPalindrome is always evaluating to true in findPal s case statement, even when it should be false.

What am I not seeing here?

最佳回答

I think you need to capitalize "True" and "False." I don t have a Haskell interpreter handy but you are probably just declaring a new variable "true" to be equal to "pal"

问题回答

Could it be that in findPal, you should write True and False instead of true and false?

EDIT: Ok, thoroughly beaten by the early bird here...





相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签