Dear friends:
I am an ULTRA-ULTRA Haskell beginner, just started literally yesterday.
I am just toying around with Cordelia Hall and John O'Donnell excellent book on dicrete mathematics with Haskell. Well, they have a program, stdm,
to accompany the book. It happens that it is in literate style. In theory, this should be very easy to work with, but after saving it with lhs extension, I try
to load it, without success. Could somebody out there help me with this?
Best regards,
Francisco Gutiérrez
From: "beginners-request@haskell.org"
To: beginners@haskell.org
Sent: Monday, April 16, 2012 2:45 PM
Subject: Beginners Digest, Vol 46, Issue 23
Send Beginners mailing list submissions to
beginners@haskell.org
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
beginners-request@haskell.org
You can reach the person managing the list at
beginners-owner@haskell.org
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: splitAt implementation (using foldr) and infinite lists
(Lorenzo Bolla)
2. Re: splitAt implementation (using foldr) and infinite lists
(Ozgur Akgun)
3. Re: Design of Webmachine in Haskell (Petar Radosevic)
4. Re: Design of Webmachine in Haskell (Michael Snoyman)
5. Re: splitAt implementation (using foldr) and infinite lists
(Dmitriy Matrosov)
6. Training tasks (Nikita Beloglazov)
7. Cross-platform .hs files on Linux and Windows (Vinay Sajip)
8. Re: Cross-platform .hs files on Linux and Windows (Lorenzo Bolla)
----------------------------------------------------------------------
Message: 1
Date: Mon, 16 Apr 2012 15:21:55 +0100
From: Lorenzo Bolla
Subject: Re: [Haskell-beginners] splitAt implementation (using foldr)
and infinite lists
Cc: beginners@haskell.org
Message-ID: <20120416142155.GC30186@dell>
Content-Type: text/plain; charset=us-ascii
On Mon, Apr 16, 2012 at 12:52:02PM +0400, Dmitriy Matrosov wrote:
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.
Try something like this:
splitAt' n = foldr (\x zs -> if fst x <= n then (snd x : fst zs, snd zs) else ([], snd x : snd zs)) ([], []) . zip [1..]
I'm no Haskell expert, but I suspect that when pattern-matching z2, it
tries to evaluate it and it hangs...
My version does not hang...
hth,
L.
--
Lorenzo Bolla
http://lbolla.info
------------------------------
Message: 2
Date: Mon, 16 Apr 2012 15:55:20 +0100
From: Ozgur Akgun
Subject: Re: [Haskell-beginners] splitAt implementation (using foldr)
and infinite lists
To: Lorenzo Bolla
Cc: beginners@haskell.org
Message-ID:
Content-Type: text/plain; charset="utf-8"
You can also use lazy pattern matching.
http://en.wikibooks.org/wiki/Haskell/Laziness#Lazy_pattern_matching
On 16 April 2012 15:21, Lorenzo Bolla wrote:
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..]
Ozgur