From: tarizzo@world-net.sct.fr (Tarizzo Martial)
To: caml-list@margaux.inria.fr
Subject: Re: curried fns
Date: Tue, 21 Nov 1995 06:17:13 +0100 [thread overview]
Message-ID: <199511210517.GAA03663@world-net.sct.fr> (raw)
>For instance, if i use the 1st form and write:
>
> let h x = let z = fact x in fun y -> y + z;;
> map (h 30) [1;2;3];; (* note 1 *)
>
>fact 30 gets evaluated only once (partial evaluation), while
>the use of the 2nd form for the h function:
>
> let h x y = let z = fact x in y + z;;
> map (h 30) [1;2;3];;
>
>causes fact 30 to be evaluated _for each_ element of the list.
>
>Is this normal or do i misunderstand sth about curryfied fns ?..
I think it's perfectly normal. One can rewrite the two definitions of h
without the syntactic sugar provided by 'in' :
* for the first one :
let h =
fun x ->
(fun z ->
(fun y -> y+z))
(fact x);;
* for the second one :
let h =
fun x ->
fun y ->
(fun z -> y+z)
(fact x);;
It's clear that the value returned by h x;; is a function of y.
In the first def, fact x is evaluated when we compute h x;; and this value
is 'captured' in the definition of fun y
In the second def, fact x is computed in the body of the function of y, i.e
each time this funciotn is called.
It's very easy to see this behavior : try to evaluate
h 30;;
with a side effect in the fact function (print its argument for example)
The first definition gives :
let h x = let z = fact x in fun y -> y + z;;
h : int -> int -> int = <fun>
#h 3;;
3
- : int -> int = <fun>
while the second one gives :
let h x y = let z = fact x in y + z;;
h : int -> int -> int = <fun>
#h 3;;
- : int -> int = <fun>
*********************************
Tarizzo Martial
Prof. Sc Physiques
Classes preparatoires
Lycee J MOULIN
57600 FORBACH
Email: tarizzo@world-net.sct.fr
74014.3307@compuserve.com
Compuserve : 74014,3307
*********************************
next reply other threads:[~1995-11-21 10:51 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1995-11-21 5:17 Tarizzo Martial [this message]
-- strict thread matches above, loose matches on Subject: below --
1995-11-20 20:32 Fauque UPS
1995-11-20 18:51 Laurent CHENO
1995-11-20 15:54 Jocelyn Serot
1995-11-20 18:06 ` John Harrison
1995-11-21 10:48 ` Pierre Weis
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=199511210517.GAA03663@world-net.sct.fr \
--to=tarizzo@world-net.sct.fr \
--cc=caml-list@margaux.inria.fr \
/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