[Haskell-beginners] Re: (->) instance for ArrowApply and laziness
aditya siram
aditya.siram at gmail.com
Fri Apr 2 14:27:59 EDT 2010
Thank you. This was incredibly helpful.
-deech
On 4/2/10, Heinrich Apfelmus <apfelmus at quantentunnel.de> wrote:
> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
More information about the Beginners
mailing list