caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Bruce Hoult <bruce@hoult.org>
To: Chris Hecker <checker@d6.com>, caml-list@inria.fr
Subject: Re: [Caml-list] currying...
Date: Tue, 6 Mar 2001 23:33:27 +1300	[thread overview]
Message-ID: <a04310117b6ca6aaacc80@[192.168.0.12]> (raw)
In-Reply-To: <4.3.2.7.2.20010306012957.00c7cf00@shell16.ba.best.com>

At 1:39 AM -0800 6/3/01, Chris Hecker wrote:
>How does caml know when to call a function?  For example, say I have:
>
>val f: int -> int -> int -> unit
>
>and the definition of f is
>
>let f x y = Printf.printf "%d %d" x y;Printf.printf "%d"
>
>so f actually takes two ints, prints them, and then returns a 
>function that takes an int and returns unit.  From the val 
>declaration above in a .cmi file, how can caml tell the difference 
>between that f and this one:
>
>let f x y z = Printf.printf "%d %d %d" x y z
>
>How does it know "when" to call f, since you need a different number 
>of parameters for the different definitions?  The top f prints x y 
>when it's called with two parms, so it doesn't wait until all three 
>parms have been passed.
>
>I have a feeling I'm missing something fundamental here, or else the 
>definition of a function internally has a field for its arity and it 
>just partially applies until it reaches the total arity.  I thought 
>I remembered seeing some documentation on this months ago, but I 
>can't find it now...
>
>It doesn't seem to partially evaluate the function or anything 
>insane like that.

Let me have an attempt at this, even though I'm a beginner too :-)


In OCaml, *all* functions actually take *one* argument.  When you write...

    let f x y = Printf.printf "%d %d" x y;Printf.printf "%d"

... it is actually just shorthand for a function that takes *one* 
argument (x) and returns as it's result a function that takes *one* 
argument (y) and prints x and y and then returns a function that 
takes one argument and prints it.

val f: int -> int -> int -> unit

.. actually means...

val f: int -> (int -> (int -> unit)))

.. it's just that since -> is right-associative you don't need the brackets.

Your first function would be perhaps better written as:

val f: int -> int -> (int -> unit)

But you don't need the parens :-)


In Scheme your first function would be like this:

(define f
   (lambda (x)
     (lambda (y)
       (display x)
       (display " ")
       (display y)
       (lambda (a)
         (display a)))))

(((f 1) 2) 3)


How does an OCaml function know how many arguments to take?  It's 
*always* one.  So you don't need the parens that you do in scheme. 
And if a function returns another function then applying it to the 
next argument in line is automatic.

This would of course be very inefficient if the compiler didn't do 
clever things...

-- Bruce
-------------------
To unsubscribe, mail caml-list-request@inria.fr.  Archives: http://caml.inria.fr


  parent reply	other threads:[~2001-03-06 10:34 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-03-06  9:39 Chris Hecker
2001-03-06 10:22 ` Thomas Colcombet
2001-03-06 10:33 ` Bruce Hoult [this message]
2001-03-06 10:50 ` Remi VANICAT
2001-03-06 16:31 ` Xavier Leroy
2001-03-06 17:41   ` Chris Hecker
2001-03-06 18:43     ` Sven LUTHER
2001-03-06 19:09     ` Marcin 'Qrczak' Kowalczyk
2001-03-06 20:14       ` Chris Hecker
2001-03-06 21:39         ` Marcin 'Qrczak' Kowalczyk
2001-03-06 23:23           ` Chris Hecker
2001-03-06 23:45             ` Marcin 'Qrczak' Kowalczyk
2001-03-07  1:10               ` Chris Hecker
2001-03-07  8:44                 ` Marcin 'Qrczak' Kowalczyk
2001-03-06 23:51             ` Chris Hecker
2001-03-06 10:16 Adam Granicz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='a04310117b6ca6aaacc80@[192.168.0.12]' \
    --to=bruce@hoult.org \
    --cc=caml-list@inria.fr \
    --cc=checker@d6.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).