is there any way for multiple where statement in Haskell

caesar_ picture caesar_ · Apr 1, 2013 · Viewed 24k times · Source

i tried to write 3-4 where statement in a one function but i get error and couldnt do it , i tried to do something like that :

foo x=
| x == foo1 = 5
| x == foo2 =3
| x == foo3 =1
| otherwise =2 
where foo1= samplefunct1 x
      foo2= samplefunct2 x
      foo3= samplefunct3 x

I know the code is a bit useless but i just wrote this to give an example about what i mean.

Is there anyone who can help me ? Thanks in advance.

Answer

gspr picture gspr · Apr 1, 2013

Remove the = after foo x and indent your code like

foo x
    | x == foo1 = 5
    | x == foo2 =3
    | x == foo3 =1
    | otherwise =2 
    where foo1 = samplefunct1 x
          foo2 = samplefunct2 x
          foo3 = samplefunct3 x

and you're fine.