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

Maciej Piechotka uzytkownik2 at gmail.com
Thu Apr 1 23:51:29 EDT 2010


On Thu, 2010-04-01 at 09:32 -0500, 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.
> 
> Thanks!
> -deech

f may be not strict in d (i.e. it may not require evaluate d to
calculate result). 

For example if you want to create graph with cycle you can write:

date Node a = Node a [Node]

myGraph x = let n = Node x [n] in n

Which is roughly equivalent to imperative:

Node n = new Node();
n.addEdgeTo(n);
return n;

It is valid as constructors are not (usually) strict in arguments.

Similarly you can write it as:

myGraph = loop ((id &&& (:[])) <<< uncurry Node)

or:

myGraph = proc (x) -> do rec n <- uncurry Node -< (n, [n])
                         returnA -< n

Usually I find let in more useful then (m)fix/loop but if code
generalise for other monads/arrows this instance is needed.

Regards
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 836 bytes
Desc: This is a digitally signed message part
Url : http://www.haskell.org/pipermail/beginners/attachments/20100401/1ea4b3a5/attachment.bin


More information about the Beginners mailing list