- On Wed, 6 Sep 2006, doug_arro wrote:

> Hi,

Same as:

>

> 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>

let foo2 x y = y + 1

> # foo2 1 ;;

The second argument is missing, so you get a function which expects the

> -: int -> int = <fun>

remaining argument.

> # 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?

>

> Thanks.

>

> Doug

>

>

>

>

>

>

> Archives up to August 22, 2005 are also downloadable at http://www.connettivo.net/cntprojects/ocaml_beginners/

> The archives of the very official ocaml list (the seniors' one) can be found at http://caml.inria.fr

> Attachments are banned and you're asked to be polite, avoid flames etc.

> Yahoo! Groups Links

>

>

>

>

>

>

>

Martin Jambon, PhD

http://martin.jambon.free.fr - 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

or similarly

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

result.

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 + 1

This 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.

--

Matt Gushee

: Bantam - lightweight file manager : matt.gushee.net/software/bantam/ :

: RASCL's A Simple Configuration Language : matt.gushee.net/rascl/ :