Currying a function to get another function: unit -> 'a
Given a higher-order function like the following:
let call (f : unit -> 'a) = f()
And another function:
let incr i = i + 1
Is there a way to pass incr
to call
, without using a lambda: (fun 开发者_JS百科() -> incr 1)
?
Obviously, passing (incr 1)
does not work, as the function is then "fully applied."
EDIT
To clarify: I'm wondering if there's a way to curry a function, such that it becomes a function: unit -> 'a
.
You can define such a shortcut yourself:
let ap f x = fun () -> f x
call (ap incr 1)
If the function you want to transform happens to be a pure function, you can define the constant function instead:
let ct x _ = x (* const is reserved for future use :( *)
call (ct (incr 1))
It looks more like an attempt to add laziness to strict F# then some kind of currying.
And in fact there is a built in facility for that in F#: http://msdn.microsoft.com/en-us/library/dd233247.aspx - lazy
keyword plus awkward Force
:
Not sure if it's any better than explicit lambda, but still:
let incr i =
printf "incr is called with %i\n" i
i+1
let call (f : unit -> 'a) =
printf "call is called\n"
f()
let r = call <| (lazy incr 5).Force
printf "%A\n" r
精彩评论