Case statements shorter

Hi, I have the following piece of code: n = 3 case n of 3 -> "Something" 4 -> "Something" _ -> "Other" Is it possible to shorten the case statement into something like this?: n = 3 case n of 3 or 4 -> "Something" _ -> "Other" Thanks. -- Angel Alonso

No, not if you use case. But you can do better with a simple if, in this
particular case.
if n == 3 || n == 4
then "Something"
else "Other"
Or even,
if n `elem` [3,4]
them "Something"
else "Other"
HTH,
On 1 November 2010 15:59, Angel Alonso
Hi,
I have the following piece of code:
n = 3 case n of 3 -> "Something" 4 -> "Something" _ -> "Other"
Is it possible to shorten the case statement into something like this?:
n = 3 case n of 3 or 4 -> "Something" _ -> "Other"
Thanks.
-- Angel Alonso _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
-- Ozgur Akgun

On Monday 01 November 2010 16:59:33, Angel Alonso wrote:
Hi,
I have the following piece of code:
n = 3 case n of 3 -> "Something" 4 -> "Something" _ -> "Other"
Is it possible to shorten the case statement into something like this?:
n = 3 case n of 3 or 4 -> "Something" _ -> "Other"
No, case does a pattern match. You can do case n of _ | n `elem` [3,4] -> "Something" | otherwise -> "Other" however, which becomes really useful if you have case expression of pat1 | cond11 -> thing11 | cond12 -> thing12 | otherwise -> thing1Other pat2 | cond21 -> thing21 ...
Thanks.
-- Angel Alonso

Hi,
I have the following piece of code:
n = 3 case n of 3 -> "Something" 4 -> "Something" _ -> "Other"
Is it possible to shorten the case statement into something like this?:
n = 3 case n of 3 or 4 -> "Something" _ -> "Other"
Is the following what you are looking for? case n of _ | n == 3 || n == 4 -> "something" _ -> "other" Some reminders: * Guards are allowed in case expressions * _ is not a special keyword in haskell. It's a regular variable so will always match. It's usually used when you don't use it's value in the right hand side or when it adds no information. In this case its value will be n so there is no reason to introduce another name. Also if you just have one special case you could use if then else: if n == 3 || n ==4 then "something" else "other" Another solution is to take the common expression out. This is useful if you are matching more complex structures in your case expression: let st = "something" in case n of Left x | x == 3 || x == 4 -> st Right _ -> st _ -> "other" Best regards.

On Mon, Nov 1, 2010 at 7:23 PM, uu1101@gmail.com
* _ is not a special keyword in haskell. It's a regular variable so will always match. It's usually used when you don't use it's value in the right hand side or when it adds no information. In this case its value will be n so there is no reason to introduce another name.
This is incorrect. _ is a pattern not a variable. I can write f (_:_) = "bla" but not f (x:x) = "bla" Conflicting definitions for `x' In the definition of `f' So _ is a pattern that always matches but is never bound to a value. Regards, Roel

Hi, I'm looking for a Red-Black Tree implementation in Haskell. I could have sworn I saw it on hackage somewhere, but now I can' t seem to find it. Does anyone know where I could find an implementation of it? ( I guess I could roll my own, but I'd rather just use a library). Thanks, Jimmy

On Tue, Nov 2, 2010 at 8:18 PM, Jimmy Wylie
I'm looking for a Red-Black Tree implementation in Haskell. I could have sworn I saw it on hackage somewhere, but now I can' t seem to find it.
Didn't Milan Straka benchmark one in his containers package? Johan

On Tue, Nov 2, 2010 at 8:37 PM, Johan Tibell
On Tue, Nov 2, 2010 at 8:18 PM, Jimmy Wylie
wrote: I'm looking for a Red-Black Tree implementation in Haskell. I could have sworn I saw it on hackage somewhere, but now I can' t seem to find it.
Didn't Milan Straka benchmark one in his containers package?
That's containers *paper*: http://research.microsoft.com/~simonpj/papers/containers/containers.pdf

On 11/2/10 2:37 PM, Johan Tibell wrote:
On Tue, Nov 2, 2010 at 8:37 PM, Johan Tibell
wrote: On Tue, Nov 2, 2010 at 8:18 PM, Jimmy Wylie
wrote: I'm looking for a Red-Black Tree implementation in Haskell. I could have sworn I saw it on hackage somewhere, but now I can' t seem to find it. Didn't Milan Straka benchmark one in his containers package? That's containers *paper*:
http://research.microsoft.com/~simonpj/papers/containers/containers.pdf
It looks like in that paper, they implemented their own Red-Black Tree-based Set, which isn't provided on hackage. It also doesn't look like they uploaded that implementation either. I guess I could use an AVL Tree, but I think a Red-Black Tree provides better insertion times, which is important for my program. Thanks for the help, Jimmy

Ralf Hinze has a paper Constructing Red-Black Trees which is cached on Citeseer http://www.comlab.ox.ac.uk/people/publications/date/Ralf.Hinze.html http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.46.2171 I thought someone turned the implementation in the paper into a library on Hackage, but I can't find it either.
participants (8)
-
Angel Alonso
-
Daniel Fischer
-
Jimmy Wylie
-
Johan Tibell
-
Ozgur Akgun
-
Roel van Dijk
-
Stephen Tetley
-
uu1101@gmail.com