is there any way for multiple where statement in Haskell
Solution 1
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.
Solution 2
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.
Solution 3
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
caesar_
Updated on May 14, 2020Comments
-
caesar_ about 4 years
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.
-
caesar_ about 11 yearsthanks 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 ?
-
Daniel Gratzer about 11 yearsyes, I just used
id
so it compiled, it doesn't matter what you stick there -
caesar_ about 11 yearswhen 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.
-
gspr about 11 yearsAre 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.
-
gspr about 11 yearsAh, in that case, it seems your code could very well be quite different from what you pasted above. Including the actual code might help.
-
caesar_ about 11 yearsyea i'm sure, i'll post what i wrote in my code now can you check it's where part ?
-
gspr about 11 yearsI 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?