
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"
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
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

Hi.
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?
You should say where you got the file, and explain what "without success" means. Also, it's relevant which version of GHC you're using (assuming you're using GHC). I googled and downloaded this file http://www.dcs.gla.ac.uk/~jtod/discrete-mathematics/Stdm.lhs I can successfully load it in both ghc-7.0.4 and ghc-7.4.1 by saying ghci -XHaskell98 Stdm.lhs The -XHaskell98 specifies that the file is written in Haskell98 rather than the more recent Haskell2010 standard. If you're using an older ghc version (ghc-6.12.3, for example), this flag isn't required. HTH, Andres
participants (2)
-
Andres Löh
-
Francisco Gutierrez