[Haskell-beginners] Re: (->) instance for ArrowApply and laziness

Heinrich Apfelmus apfelmus at quantentunnel.de
Fri Apr 2 13:24:40 EDT 2010


aditya siram wrote:
> Hi all,
> Could someone help me understand how the (->) instance for ArrowApply
> works? It looks like this:
>> instance ArrowLoop (->) where
>>         loop f b = let (c,d) = f (b,d) in c
> 
> This models recursion for arrows and I need help understanding how it
> does that. I have a strong feeling it has to do with the magic value
> 'd'. I've read the Patterson tutorial but I still don't grok it.

It's similar to how  fix  works:

  http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion

Here various definitions of the factorial as example:

1) Definition

    factorial n = product [1..n]

2) Recursive definition

    factorial 0 = 1
    factorial n = n * factorial (n-1)

3) Recursion with the fixed point combinator  fix

    fix f = let knot = f knot in knot

    factorial = fix fac
        where
        fac f 0 = 1
        fac f n = n * f (n-1)

4) Recursion with  loop

    factorial = loop helper
        where
        fac f 0 = 1
        fac f n = n * f (n-1)

        helper (n,knot) = (knot n, fac knot)


Regards,
Heinrich Apfelmus

--
http://apfelmus.nfshost.com



More information about the Beginners mailing list