Case
From HaskellWiki
(Difference between revisions)
(more flavors) 
(→Using functions: reshuffle some code lines, c/e) 

(5 intermediate revisions by 3 users not shown)  
Line 8:  Line 8:  
=== Using functions === 
=== Using functions === 

+  
+  ==== select ==== 

We can do this nicely with a function implemented in Haskell: 
We can do this nicely with a function implemented in Haskell: 

Line 13:  Line 15:  
select :: a > [(Bool, a)] > a 
select :: a > [(Bool, a)] > a 

select def = maybe def snd . List.find fst 
select def = maybe def snd . List.find fst 

−  +   = fromMaybe def . lookup True 

+   = maybe def id . lookup True 

select exDefault 
select exDefault 

Line 21:  Line 23:  
</haskell> 
</haskell> 

Unfortunately this function is not in the [[Prelude]]. 
Unfortunately this function is not in the [[Prelude]]. 

+  It is however in the [http://hackage.haskell.org/packages/archive/utilityht/0.0.1/doc/html/DataBoolHT.html#v%3Aselect utilityht] package. 

+  
+  ==== nested 'if' ==== 

Alternative implementations are 
Alternative implementations are 

<haskell> 
<haskell> 

−  select' def = fromMaybe def . lookup True 

−  
{ a purely functional implementation of ifthenelse } 
{ a purely functional implementation of ifthenelse } 

if' :: Bool > a > a > a 
if' :: Bool > a > a > a 

Line 44:  Line 47:  
exDefault 
exDefault 

</haskell> 
</haskell> 

+  
+  ==== infix operator ==== 

If you use <hask>if'</hask> in infix form, 
If you use <hask>if'</hask> in infix form, 

Line 59:  Line 64:  
</haskell> 
</haskell> 

+  === Using syntactic sugar === 

−  +  ==== Guards ==== 

−  === Using syntactic sugar === 

You can make use of some [[syntactic sugar]] of Haskell, namely of [[guard]]s. 
You can make use of some [[syntactic sugar]] of Haskell, namely of [[guard]]s. 

Line 74:  Line 80:  
Alternatively, one could simply factor out a function(/value) and use guards in the argument patterns. 
Alternatively, one could simply factor out a function(/value) and use guards in the argument patterns. 

+  ==== List comprehensions ==== 

+  
+  An alternative sugarful approach is to use [[list comprehension]]s. 

+  
+  <haskell> 

+  head $ 

+  [ ex1  cond1 ] ++ 

+  [ ex2  cond2 ] ++ 

+  [ ex3  cond3 ] ++ 

+  [ exDefault ] 

+  </haskell> 

[[Category:FAQ]] 
[[Category:FAQ]] 
Latest revision as of 18:31, 15 March 2012
Contents 
[edit] 1 Question
Can I have acase
[edit] 2 Answer
There are several approaches to this problem.
[edit] 2.1 Using functions
[edit] 2.1.1 select
We can do this nicely with a function implemented in Haskell:
select :: a > [(Bool, a)] > a select def = maybe def snd . List.find fst  = fromMaybe def . lookup True  = maybe def id . lookup True select exDefault [(cond1, ex1), (cond2, ex2), (cond3, ex3)]
Unfortunately this function is not in the Prelude. It is however in the utilityht package.
[edit] 2.1.2 nested 'if'
Alternative implementations are
{ a purely functional implementation of ifthenelse } if' :: Bool > a > a > a if' True x _ = x if' False _ y = y select'' = foldr (uncurry if')
select''
select
if
if'
zipWith3
zipWith3 if'
See ifthenelse.
Alternatively you can unrollfoldr
if' cond1 ex1 $ if' cond2 ex2 $ if' cond3 ex3 $ exDefault
[edit] 2.1.3 infix operator
If you useif'
?
then because of partial application it will work nicely together with '$' for the else clause.
infixl 1 ? (?) :: Bool > a > a > a (?) = if' cond1 ? ex1 $ cond2 ? ex2 $ cond3 ? ex3 $ exDefault
[edit] 2.2 Using syntactic sugar
[edit] 2.2.1 Guards
You can make use of some syntactic sugar of Haskell, namely of guards.
case () of _  cond1 > ex1  cond2 > ex2  cond3 > ex3  otherwise > exDefault
Alternatively, one could simply factor out a function(/value) and use guards in the argument patterns.
[edit] 2.2.2 List comprehensions
An alternative sugarful approach is to use list comprehensions.
head $ [ ex1  cond1 ] ++ [ ex2  cond2 ] ++ [ ex3  cond3 ] ++ [ exDefault ]