Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: (->) instance for ArrowApply and laziness (Heinrich Apfelmus)
2. Re: Question about IO, particularly hGetContents (Isaac Dupree)
3. Re: Re: (->) instance for ArrowApply and laziness (aditya siram)
4. Re: (->) instance for ArrowApply and laziness (Heinrich Apfelmus)
----------------------------------------------------------------------
Message: 1
Date: Fri, 02 Apr 2010 19:24:40 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: (->) instance for ArrowApply and
laziness
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
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
------------------------------
Message: 2
Date: Fri, 02 Apr 2010 14:13:39 -0400
From: Isaac Dupree <[email protected]>
Subject: Re: [Haskell-beginners] Question about IO, particularly
hGetContents
To: "[email protected]" <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 04/02/10 10:28, Kyle Murphy wrote:
> I'm a bit of a beginner so I might be wrong, but I think do only forces
> evaluation at the level of the do block, not recursively. Think of it this
> way, you've got a series of function calls, which are represented as thunks,
> and which in turn return more thunks. If you use do notation to execute that
> series of functions they all get evaluated in order, but you're still left
> with more un-evaluated thunks because that's what the functions returned.
The only reason hGetContents was able to return thunks with
not-yet-completed IO is because it cheated and used unsafeInterleaveIO.
"hGetContents" is one of the worst functions you could possibly pick
to learn the basic semantics of Haskell IO.
Normally, an IO-action being executed in sequence will force all the
input and output it contains to complete, though some pure computation
might remain not-yet-evaluated (say, sorting or summing a list that
you'd retrieved from IO...)
-Isaac
------------------------------
Message: 3
Date: Fri, 2 Apr 2010 13:27:59 -0500
From: aditya siram <[email protected]>
Subject: Re: [Haskell-beginners] Re: (->) instance for ArrowApply and
laziness
To: Heinrich Apfelmus <[email protected]>
Cc: [email protected]
Message-ID:
<[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Thank you. This was incredibly helpful.
-deech
On 4/2/10, Heinrich Apfelmus <[email protected]> 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
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 4
Date: Sat, 03 Apr 2010 15:39:32 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: [Haskell-beginners] Re: (->) instance for ArrowApply and
laziness
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
aditya siram wrote:
> Heinrich Apfelmus wrote:
>>
>> It's similar to how fix works:
>>
>> http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion
>>
>> Here various definitions of the factorial as example:
>
> Thank you. This was incredibly helpful.
> -deech
Anytime. :)
There's also a humorous but enlightening list of increasingly complex
implementations of the factorial function:
The Evolution of a Haskell Programmer.
http://www.willamette.edu/~fruehr/haskell/evolution.html
The version with fix corresponds to the "Boy Scout Haskell programmer".
Regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 22, Issue 4
****************************************