Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'where' inside other expression

Tags:

haskell

I can use let inside other expression.

foo n = (let a = True in (\x -> a)) 3

foo' n | n == 1 = let a = True in a
       | n /= 1 = False

But I can't do the same with where

foo n = ((\x -> a) where a = True) 3

foo' n | n == 1 = a where a = True
       | n /= 1 = False

1:20: parse error on input `where'

Is it really impossible in haskell or just my mistake?

like image 433
Nelson Tatius Avatar asked Dec 06 '22 11:12

Nelson Tatius


1 Answers

let is an expression while where is a clause. where is bound to syntactic constructs, let can be used anywhere expressions can.

You could of course write it like this:

foo n = ((\x -> a)) 3 where a = True

foo' n | n == 1 = a
       | n /= 1 = False
       where a = True

or like this:

foo n = (\a -> (\x -> a) 3) True
like image 116
Pubby Avatar answered Dec 09 '22 15:12

Pubby