because that's how you defined it. I guess you want

1
let x y = x() + y()
By on 1/17/2011 10:17 AM ()

Thank you for your answer. The evaluating of an unit parameter is from the Thoughts on Catamorphisms Part 3 F# and #NET. The url is:

[link:lorgonblog.wordpress.com]

The code in question is:

1
2
3
4
5
6
7
8
9
10
let Eval expr =  
    // f : Expr -> unit -> int 
    let f = FoldExpr (fun x () -> x) 
                     (fun l op r () -> match op with 
                                       | Plus -> l() + r() 
                                       | Minus -> l() – r()) 
                     (fun c t e () -> if c() <> 0 then t() else e()) 
                     (fun e () -> let x = e() 
                                  printf "<%d>" x 
                                  x) 

So going back to my original question, why does let x a () = a evaluate to () -> 'a?

By on 1/17/2011 3:06 PM ()

One of my all-time favorite blogs :)

As it says in the blog

It almost works, but notice that the third expression – the one with the if-then-else – ended up "executing" both the "then" branch and the "else" branch, printing both "<42>" and "<0>" while evaluating the expression. Presumably this is not the desired semantic for our little language! Since FoldExpr handles the recursion and always recurses to traverse the whole tree, it isn’t obvious how to fix this. But in fact, we just use a trick we already saw in the last installment. Currently FoldExpr returns an "int", and thus each recursive call fully evaluates things to yield a final result. We can easily change it so that instead of returning an int, the FoldExpr call will return a function "unit -> int" – a function that is waiting to be invoked to produce a result. This changes the type of all the recursive parts, and gives us control over when code is actually executed. Here’s the new code:

...

We’ve added one extra "unit" argument to each function passed to FoldExpr. As a result, the types change so that entities like "c", "t", and "e" in the third function no longer have type "int"; instead they have type "unit -> int". Thus the code we write in the body of the third function can choose when&whether to call each of these functions. We call c(), and based on its value, either call t() or call e(). As a result, in our third example expression, only the print in the "then" branch gets executed ("<42>" is printed but "<0>" is not). Just what we want! Note that in the last line of the Eval function, we had to call the result of the FoldExpr (which I named "f" above) with not just the expression, but also a unit in order to ‘kick off’ the execution of the evaluation.

Anyway, in case it helps answer your question

1
    let f x () = x

means the same thing as

1
    let f x (unitArg:unit) = x

which is to say that f is a two-argument function that takes anything ('x') as its first parameter, and an object of type unit ('()') as its second parameter.

By on 1/17/2011 3:35 PM ()

Thank you now I understand

By on 1/18/2011 3:11 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper