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

Tom Murphy amindfv at gmail.com
Mon Dec 12 00:54:48 CET 2011


To clarify, by hack I meant that it seemed like a workaround specifically
to keep "case" in the OP's code, when it seemed like they were looking for
the functionality of guards.

amindfv / Tom
On Dec 11, 2011 1:39 PM, "Yitzchak Gale" <gale at sefer.org> wrote:

> Brandon Allbery wrote:
> >>> > case () of
> >>> >   () | s == reverse s -> putStrLn "palindrome"
> >>> >   _                   -> putStrLn "nope"
>
> Tom Murphy wrote:
> >> This is kind of a hack of case, though. I think what the OP was looking
> >> for is
> >>  isPalindrome word
> >>   | (word == reverse word) = putStrLn (word ++ " is a palindrome")
> >>   | otherwise              = putStrLn (word ++ " is not a palindrome")
>
> > Erm?  It's as much of a hack of case as yours is, since the above is
> > actually using case.
>
> I agree with Tom here. While it's true that the compiler
> internally desugars to case, that low-level compiler
> transformation doesn't have much to do with the
> best way to write clear code.
>
> I find that case often creates code that is more
> confusing and bug-prone. Except when what I
> really want to express is pattern matching, *and*
> there is some specific reason here why I don't
> want to use a named function in a let or where
> binding. Altogether, it doesn't come up very often
> for me.
>
> And even for styles that use case more than I
> do, certainly there is room to call the use of
> the "case ()" idiom a hack. (Even though I'll
> admit that I do use it sometimes.)
>
> Regards,
> Yitz
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20111211/0ac3c633/attachment-0001.htm>


More information about the Haskell-Cafe mailing list