is there any way for multiple where statement in Haskell
Asked Answered
R

3

22

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.

Resendez answered 1/4, 2013 at 9:5 Comment(4)
There shouldn't be an = after foo x.Whinny
@Whinny yea you right sorry about that but , i think it wasnt the real question.Resendez
Also, I suggest that you in the future include any error you get. Simply telling people that you "get an error" is not constructive.Whinny
I'll rollback your edit to the original version because that's what the answers respond to. :)Heliogravure
W
38

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.

Whinny answered 1/4, 2013 at 9:11 Comment(5)
when i wrote someting like i get an error saying "Syntax error in input (unexpected `=') " my where statement part is |otherwise = (-1,-1) where rightk = rightCheck area number leftk = leftCheck area number and when i delete one of the where statements, it runs correctly.Resendez
Are you sure your whitespaces are correct? It's hard to tell what you mean by code "like" mine. What I pasted does not have syntax errors.Whinny
Ah, in that case, it seems your code could very well be quite different from what you pasted above. Including the actual code might help.Whinny
yea i'm sure, i'll post what i wrote in my code now can you check it's where part ?Resendez
I can't check whitespace/indenting when your code is in a comment. Moreover, are you sure that the syntax error is not elsewhere in the code? Have you checked the line number?Whinny
H
14

If your indentation is a bit uneven, like this:

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

then indeed, the error message talks about unexpected = (and in the future, please do include full error message in the question body).

You fix this error by re-aligning, or with explicit separators { ; }, making it white-space–insensitive:

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

This runs fine (not that it is a nice style to use). Sometimes it even looks even to you, but isn't, if there are some tab characters hiding in the white-space.

Heliogravure answered 1/4, 2013 at 13:12 Comment(0)
C
12

This code is almost right, you just need the correct indentation: Whitespace matters in haskell. Additionally, using an = after foo is an error with guards, so you'll have to remove that as well. The result is:

foo x
  | x == foo1 = 5
  | x == foo2 =3
  | x == foo3 =1
  | otherwise =2 
  where foo1= whatever1 x
        foo2= whatever2 x
        foo3= whatever3 x
Caribbean answered 1/4, 2013 at 9:11 Comment(2)
thanks but i want to use 3 different functions in each where statement.As you can see from my code the first where is running with samplefunc1 and the second is running with samplefunct2 and so on, are u sure it is okey with that ?Resendez
yes, I just used id so it compiled, it doesn't matter what you stick thereCaribbean

© 2022 - 2024 — McMap. All rights reserved.