[Haskell-cafe] Why doesn't this work? (palindrome :: IO)

Tom Murphy amindfv at gmail.com
Thu Dec 8 21:52:17 CET 2011


On Wed, Dec 7, 2011 at 11:46 PM, Brandon Allbery <allbery.b at gmail.com>wrote:

> On Wed, Dec 7, 2011 at 23:24, Alexej Segeda <aloscha_den_store at hotmail.com
> > wrote:
>
>>                 case s of
>>                    (s == reverse s)    -> putStrLn (s ++ " is a
>> palindrome")
>>                    otherwise           -> putStrLn (s ++ " is not a
>> palindrome")
>>
>
> case does pattern matching, not Boolean expressions.  (s == reverse s) is
> not a useful pattern, and in fact is probably a syntax error because ==is not a valid infix constructor.
>
> If you want to do Boolean comparisons in a case, you need to use
> something like
>
> > case () of
> >   () | s == reverse s -> putStrLn "palindrome"
> >   _                   -> putStrLn "nope"
>
>
>

This is kind of a hack of case, though. I think what the OP was looking for
is

palindrome :: IO ()
palindrome = do putStrLn "Type in a word"
                s <- getLine
                isPalindrome s

isPalindrome word
  | (word == reverse word) = putStrLn (word ++ " is a palindrome")
  | otherwise              = putStrLn (word ++ " is not a palindrome")


amindfv / Tom
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111208/fd87a272/attachment.htm>


More information about the Haskell-Cafe mailing list