Note that the parallel to Javascript isn't quite correct -- for instance, assuming a
returns a number, b + b
makes sense in Haskell but not in your example Javascript. In principle functions in Haskell are functions of exactly one argument -- what appears to be a function of two arguments is a function of one argument, which returns a function of one argument, which returns a value. b
in Haskell would not be an unevaluated "zero-argument function", but an unevaluated value.
To introduce strictness you can use seq
, which takes two arguments, the first of which is strictly evaluated and the second of which is returned. Read more.
Here is an example from that page where seq
is used to force immediate evaluation of z'
:
foldl' :: (a -> b -> a) -> a -> [b] -> a
foldl' _ z [] = z
foldl' f z (x:xs) = let z' = f z x in z' `seq` foldl' f z' xs
Note the way z'
is used again later as the second argument to foldl'
, since seq
just discards the value of its first argument.