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.  splitAt implementation (using foldr) and infinite lists
      (Dmitriy Matrosov)
   2. Re:  Tying the knot with binary trees (Michael Schober)


----------------------------------------------------------------------

Message: 1
Date: Mon, 16 Apr 2012 12:52:02 +0400
From: Dmitriy Matrosov <[email protected]>
Subject: [Haskell-beginners] splitAt implementation (using foldr) and
        infinite lists
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi all.

If i implement take using foldr

take'       :: Int -> [a] -> [a]
take' n     =  foldr (\x z -> if fst x <= n then snd x : z else []) []
                . zip [1..]

it'll work fine with infinite lists. But if i implement splitAt similarly

splitAt'    :: Int -> [a] -> ([a], [a])
splitAt' n  = foldr (\x (z1, z2) -> if fst x <= n then  (snd x : z1, z2)
                                       else              ([], snd x : z2))
                     ([], [])
                     . zip [1..]

and call it like this

*Main> fst $ splitAt' 4 [1..]
^CInterrupted.

it will hang. I don't understand, why it tries to compute second list 
(z2), if it only needs first?





------------------------------

Message: 2
Date: Mon, 16 Apr 2012 11:17:19 +0200
From: Michael Schober <[email protected]>
Subject: Re: [Haskell-beginners] Tying the knot with binary trees
To: Markus L?ll <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed

Hi Markus,

> They say that for this or similar reasons you have to use a zipper,
> but I know nothing more about them than this :-)

Thanks for the hint. I hadn't heard of zippers before and after some 
reading, they indeed seem to do the trick, although I have yet to 
implement it in my solution.

Anyways, here are the resources which worked best for my understanding:

[1] http://en.wikibooks.org/wiki/Haskell/Zippers
[2] http://learnyouahaskell.com/zippers

Best,
Michael






------------------------------

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 46, Issue 22
*****************************************

Reply via email to