[Haskell-beginners] Data.Stream interleave implementation question
Julian Birch
julian.birch at gmail.com
Mon Aug 18 20:24:23 UTC 2014
I had the exact same problem. It's a consequence of "pattern matching
drives evaluation", and indeed that's the lesson I learned from the
exercise. If you match them together at the start, you end up with a
recursive call before you've generated the first item in the list. If you
just restructure it so that the second destructure happens after you've
produced the first value, everything works.
Julian.
On 18 August 2014 19:22, Curt McDowell <geek at fishlet.com> wrote:
> Funny, I was trying the same Homework, implemented interleaveStreams using
> the same algorithm you did (taking one item from each stream per recursive
> call), and got the same result with the ruler function hanging. It was also
> fixed by changing interleaveStreams to take from just one stream per call
> and switch back and forth between streams.
>
> This happened even though I implemented my ruler function itself
> recursively:
>
> ruler' :: Integer -> Stream Integer
> ruler' n = interleaveStreams (streamRepeat n) (ruler' (n + 1))
>
> ruler = ruler' 0
>
> Sorry, but I also don't know why. I speculate that if one were to expand it
> out on pencil and paper, they'd find that the interleaved stream somehow
> expands at a different rate than the ruler (half or twice).
>
> -Curt
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140818/84f1000b/attachment-0001.html>
More information about the Beginners
mailing list