Question

When I use JS, I have two options to handle a function.

var a = function() {};
var b = a;     // b is the function a itself.
var c = a();   // c is result of the evaluation of function a.

AFAIK, Haskell is lazy by default, so I always get b by default. But if I want to get c, how can I do?

Update

I think I should put a word explicitly.

I was doing something like this in ghci.

let a = getLine
a

I wanted to let a result of getLine into a.

Update2

I note this code for later reference for people like me. I could correct translation to Haskell with @Ankur's help. With above code example is not a good one because function a doesn't return anything. If I change it like this;

var a = function(x,y) { return x * y; };
var b = a;     // b is the function a itself.
var c = a();   // c is result of the evaluation of function a.

Translation into Haskell will become like this.

let a = \ x y -> x* y    // Anonymous lambda function.
let b = a
let c = a 100 200        
Was it helpful?

Solution

Your JS code would translate to Haskell as:

Prelude> let a = (\() -> ())
Prelude> let b = a
Prelude> let c = a()

Your JS function was taking Nothing (which you can model as () type) and returning nothing i.e again ()

getLine is a value of type IO String so if you say let a = getLine, a becomes value of type IO String. What you want is extract String from this IO String, which can be done as:

a <- getLine

OTHER TIPS

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.

  1. Haskell is non-strict, not lazy.
  2. Many expressions will be evaluated strictly, see here, so you can often force strictness simply with the structure of your code.
  3. In Haskell, if c has a type which matches the return type - the value - of a() then that is what will be assigned to it (never the function itself).
  4. Haskell may put off the calculation until your code actually needs the value of c but in most cases you should not care.

Why do you want to force the evaluation early? Usually, the only reason to do this in Haskell is performance. In less pure languages, you might be depending on a side effect but that will not be the case in Haskell - unless you're working with, say, the IO monad and that gives you all you need to force sequential evaluation.

UPDATE Ah, so you are working with IO.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top