Version française
Home     About     Download     Resources     Contact us    
Browse thread
Immediate recursive functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Jason Hickey <jyh@c...>
Subject: Re: [Caml-list] Immediate recursive functions
Alex Baretta wrote:
> I sometimes feel the need for a mu operator. I'm thinking of something 
> like the following:
> 
> # (rec f x -> if x <= 0 then 1 else x * (f (pred x))) 5
> - : int = 120
...
> This is not really a feature wish so much a bit of insane curiosity. Is 
> there any specific reason for not having this in the core language syntax?

I can't give any arguments why your specific syntax is not allowed.  In 
principle it isn't necessary, since a general fixpoint can be defined.

# let rec fix f x = f (fix f) x;;
val fix : (('a -> 'b) -> 'a -> 'b) -> 'a -> 'b = <fun>
# fix (fun f i -> if i = 0 then 1 else i * f (i - 1)) 10;;
- : int = 3628800
# fix (fun f i j -> if i = 0 then 1 else j * f (i - 1) j) 10 5;;
- : int = 9765625

The drawback is that (fix f x) is likely to be a bit more expensive to 
evaluate than the let-rec version.

Jason

-- 
Jason Hickey                  http://www.cs.caltech.edu/~jyh
Caltech Computer Science      Tel: 626-395-6568 FAX: 626-792-4257