On Mon, May 28, 2007 at 08:10:11AM +0900, Jon Harrop wrote:
> 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?
inject is a fold. yield is not a continuation, but rather a way of
accessing the passed in function (block) anonymously.

def f
yield
end

def f1(&b)
b.call
end

f { puts "Does the same thing" }
f1 { puts "Does the same thing" }

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