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: Data.Stream interleave implementation question (Isaac Dupree)
2. Re: Data.Stream interleave implementation question (Kim-Ee Yeoh)
----------------------------------------------------------------------
Message: 1
Date: Wed, 12 Feb 2014 03:36:41 -0500
From: Isaac Dupree <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Data.Stream interleave implementation
question
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
On 02/11/2014 11:23 PM, Bryan Brady wrote:
> [...]
> interleaveStreams :: Stream a -> Stream a -> Stream a
> interleaveStreams (Cons a as) (Cons b bs) = Cons a (Cons b
> (interleaveStreams as bs))
>
> ruler :: Stream Integer
> ruler = foldr1 interleaveStreams (map streamRepeat [0..])
> [...]
> In the latter definition, Cons a (interleaveStreams bs as),
> (interleaveStreams bs as) is a thunk. The thunk should only be evaluated
> when it is needed. In my original definition, (interleaveStreams as bs)
> is a thunk. The difference is an extra Cons (e.g., Cons b). It seems
> like the additional Cons is causing problems, I just don't understand
> why. Can anyone point out what I'm missing?
The issue is on the left-hand-side of interleaveStreams. It is strict
in both its arguments. Look at foldr1; you're evaluating
interleaveStreams (streamRepeat 0)
(interleaveStreams (streamRepeat 1)
(interleaveStreams (streamRepeat 2)
(interleaveStreams (streamRepeat 3)
...
In this situation, if interleaveStreams evaluates its second argument
before returning any work, it will never be able to return. Happily,
the outer Cons of the result does not depend on the second argument. I
can fix the issue just by making the second argument be pattern-matched
lazily (with ~, i.e. only as soon as any uses of the argument in the
function are evaluated):
interleaveStreams (Cons a as) ~(Cons b bs) = Cons a (Cons b
(interleaveStreams as bs))
You can also write this without ~ by explicitly pattern matching later, e.g.
interleaveStreams (Cons a as) bs = Cons a (case bs of Cons b bs' -> Cons
b (interleaveStreams as bs'))
I'm not sure whether there's a practical difference between these and
Data.Stream's definition. Actually, I think they turn out to do exactly
the same thing...
(I tested by deriving Show for Stream, and typing 'ruler' in `ghci
file.hs`, or 'take 80 (show ruler)'.)
-Isaac
------------------------------
Message: 2
Date: Wed, 12 Feb 2014 16:11:08 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Data.Stream interleave implementation
question
Message-ID:
<capy+zdsmljxxkefveegfj0x7utowv-_dmk1cdpvsmjavx+c...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
On Wed, Feb 12, 2014 at 11:23 AM, Bryan Brady <[email protected]> wrote:
> In the latter definition, Cons a (interleaveStreams bs as),
> (interleaveStreams bs as) is a thunk. The thunk should only be evaluated
> when it is needed. In my original definition, (interleaveStreams as bs) is
> a thunk. The difference is an extra Cons (e.g., Cons b). It seems like the
> additional Cons is causing problems, I just don't understand why. Can
> anyone point out what I'm missing?
As Isaac wrote, you want to look at the _left_ hand side, not the right.
It's pattern matching that impacts operational semantics.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140212/05db6d5f/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 68, Issue 10
*****************************************