Logan Capaldo wrote:
> The closest to the OCaml would look like:
> 
>   def nest(x, n = 2, &f)
>     if n == 0 then x else nest(f[x], n - 1, &f) end
>   end

Ok, that's easily the best I've seen in any other language.

> nest(-3) { |a| a + 1 } #=> -1

So nest(-3) defaults to nest(-3, 2) and returns a closure that accepts the
anonymous function { |a| a + 1 } and applies it to -3 twice, is that right?

Back in OCaml, that is:

  nest ((+) 1) (-3)

> However, I'd probably write it like:
>   def nest(x, n = 2)
>     (1..n).inject(x) { |acc, _| yield(acc) }
>   end

I don't understand this one. I think "inject" is a fold and "yield" returns
a value and a continuation. Looks like the continuation is ignored the next
time it is accumulated, but won't the result have a continuation in it?

> nest("a", 3) { |a| a + a } #=> "aaaaaaaa"

I think this is:

  nest ~n:3 (fun a -> a^a) "a"

Thanks!

-- 
Dr Jon D Harrop, Flying Frog Consultancy
The F#.NET Journal
http://www.ffconsultancy.com/products/fsharp_journal/?usenet