foldr (+) 0 [1,2] returns 3
How could I write it using 'where'?
foldr f 0 [1,2] where f = (+) returns "parse error on input 'where'"
Edit:
actually I’m trying to make cartesian product as in the example
cprod = foldr f [[ ]]
where f xs yss = foldr g [ ] xs
where g x zss = foldr h zss yss
where h ys uss = (x : ys) : uss
but, this again gives parse error on input where, when I’m trying to load the file.
... where ...is not an expression.whereis associated with bindings, not expressions.You could use
let:let f = (+) in foldr f 0 [1,2], or you could use it with some binding:x = foldr f 0 [1,2] where f = (+).The following is a syntactically valid version of your edited code (it’s still broken, but that’s not a syntactic issue anymore 🙂 ). You only want to use one
whereper binding, and you want thewhereto be more indented than the body of the function.Looking again, I see that I misunderstood your code — you had three
wheres because you wanted each one to apply to the previous functions. In that case you have to indent thewheres the way I said, e.g.If you insist on using
wherelike this and don’t like deeply indented code, you can use explicit{}/;rather than layout. An extreme case would beBut this isn’t usually considered good style.