Re: "ocaml_beginners":: How does Ocaml deduce function formal parameters?
- On Wed, 6 Sep 2006, doug_arro wrote:
> Hi,This is because foo2 is equivalent to:
> Can someone please enlighten me on how Ocaml decudes formal
> parameters for a function? See the following example:
> # let foo = function x -> x + 1;;
> val foo : int -> int = <fun>
> # foo 1 ;;
> -: int =2 ;;
> # let foo2 x = function x -> x + 1;;
> val foo2 : 'a -> int -> int = <fun>
> # foo2 1 ;;
> -: int -> int = <fun>
> # let foo3 x = x + 1 ;;
> val foo3 : int -> int = <fun>
> # foo3 1 ;;
> - : int = 2
> Why do foo and foo3 compute to the same value when being applied to 1
> while foo2 computes to a function instead?
let foo2' x = function y -> y + 1
let foo2'' x y = y + 1
The parameter x here (and the first x in your foo2) is unused in the inner
computation. So what happens here is just what the code looks like, you
pass something (anything) into foo2, and that something, whatever yt was,
is thrown away, and the function (function y -> y+1) is returned as a
William D. Neumann
"There's just so many extra children, we could just feed the
children to these tigers. We don't need them, we're not doing
anything with them.
Tigers are noble and sleek; children are loud and messy."
-- Neko Case
Life is unfair. Kill yourself or get over it.
-- Black Box Recorder
- Martin Jambon wrote:
> let foo2 x y = y + 1This is known as partial evaluation, and it can be quite useful.
> > # foo2 1 ;;
> > -: int -> int = <fun>
> The second argument is missing, so you get a function which expects the
> remaining argument.
: Bantam - lightweight file manager : matt.gushee.net/software/bantam/ :
: RASCL's A Simple Configuration Language : matt.gushee.net/rascl/ :