[Haskell-cafe] Haskell Quick Reference (1-page PDF)
Joachim Breitner
mail at joachim-breitner.de
Thu Nov 6 10:48:16 EST 2008
Hi,
the reference suggests the use of otherwise (instead of _) as the
default pattern in a case expression. While it certainly works, isn’t it
bad style, as it shadows Prelude.otherwise:
$ cat otherwise.lhs ; runhaskell otherwise.lhs
>demo b arg = case b of
> True -> do print "First arg was True"
> let g | arg == "Something" = print "Got something"
> | otherwise = print "Got anything else"
> g
> otherwise -> do print "First arg was not True"
> let g | arg == "Something" = print "Got something"
> | otherwise = print "Got anything else"
> g
>
>main = do demo True "Something"
> demo True "Something else"
> demo False "Something"
> demo False "Something else"
"First arg was True"
"Got something"
"First arg was True"
"Got anything else"
"First arg was not True"
"Got something"
"First arg was not True"
otherwise.lhs: otherwise.lhs:(7,36)-(8,86): Non-exhaustive patterns in function g
I know that this is a contrieved example (using case on a boolean), but
in other cases you’ll get strange type errors. So while I think I did it
in the past, otherwise should be avoided in case expressions.
Greetings,
Joachim
--
Joachim "nomeata" Breitner
mail: mail at joachim-breitner.de | ICQ# 74513189 | GPG-Key: 4743206C
JID: nomeata at joachim-breitner.de | http://www.joachim-breitner.de/
Debian Developer: nomeata at debian.org
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: Dies ist ein digital signierter Nachrichtenteil
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20081106/47028612/attachment.bin
More information about the Haskell-Cafe
mailing list