hello, i'd like to suggest that the definition of "intersperse" from the List module be made more lazy. in the hugs prelude (and GHC exhibits the same behaviour) we are given the following defn: intersperse :: a -> [a] -> [a] intersperse _ [] = [] intersperse _ [x] = [x] intersperse sep (x:xs) = x : sep : intersperse sep xs the second equation makes the defn, strict in the tail of the list. this is a probelm when processing lazy lists (e.g. things coming over a network), as one gets all events with a delay. here is an alternative definition: intersperse :: a -> [a] -> [a] intersperse _ [] = [] intersperse sep (x:xs) = x : rest where rest [] = [] rest xs = sep : intersperse sep xs bye iavor -- ================================================== | Iavor S. Diatchki, Ph.D. student | | Department of Computer Science and Engineering | | School of OGI at OHSU | | http://www.cse.ogi.edu/~diatchki | ==================================================
On Tue, 02 Mar 2004 16:59:48 -0800, Iavor S. Diatchki <diatchki@cse.ogi.edu> wrote:
i'd like to suggest that the definition of "intersperse" from the List module be made more lazy.
Good thing, I think that library functions should always be as lazy as possible in their observeable interface (or well documented why they aren't)
intersperse :: a -> [a] -> [a] intersperse _ [] = [] intersperse sep (x:xs) = x : rest where rest [] = [] rest xs = sep : intersperse sep xs
I don't like the generic name "rest" so much, and the function is not as efficient as it could be due to too much matching -- what about:
intersperse :: a -> [a] -> [a] intersperse sep [] = [] intersperse sep (x:xs) = x : prefix sep xs
prefix :: a -> [a] -> [a] prefix sep [] = [] prefix sep (x:xs) = sep : x : prefix sep xs
-- Daan.
participants (2)
-
Daan Leijen -
Iavor S. Diatchki