Version française
Home     About     Download     Resources     Contact us    
Browse thread
Re: curried fns
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: tarizzo@w...
Subject: Re: curried fns

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