Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

[Haskell] Case statement and keyword "otherwise"

26 views
Skip to first unread message

rincewind

unread,
Feb 20, 2007, 1:53:34 PM2/20/07
to
Accidentally, I wrote something like:

fun x = case x of 0 -> "zero"; 1 -> "one"; otherwise -> "many"

By "otherwise" I meant "_". Surprisingly, it seems to work how I intended.
Now I don't understand how - taking into account that "otherwise" has Bool
value True, there must be type error here! Or is this word means something
different when inside a case statement?


Henrik Huttunen

unread,
Feb 20, 2007, 2:21:20 PM2/20/07
to
Hi.

I don't know Haskell, but in some languages (f.ex. Scala) 'otherwise' is
seen just an identifier for an immutable value that is bound to anything
x happens to be. The _ is just a special case that skips the binding.
If you have 'otherwise' already defined in same scope, lowercased
identifier in match will become a new, shadowing identifier (not sure if
'shadow' is right term).

- Henrik


rincewind

unread,
Feb 20, 2007, 3:07:40 PM2/20/07
to
Oops, never mind my question, I got it - "otherwise" in that case's just a
pattern, which matches all "default" cases.

"rincewind" <fa...@not.real> wrote in message
news:erfgcv$1s9s$1...@news.mtu.ru...

0 new messages