[Haskell-beginners] defining 'init' in terms of 'foldr'

Paul Higham polygame at mac.com
Sat Dec 4 23:20:51 CET 2010


Not sure if this thread is still active but I also struggled with this  
same exercise.  I offer the following solution as a thing to shoot at:

myInit :: [a] -> [a]
myInit ys = foldr snoc [] $ (\(x:xs) -> xs) $ foldr snoc [] ys
	    where snoc = (\x xs -> xs ++ [x])

Note that snoc is defined at the top of the same page as the exercise  
in Simon's book.

::paul



More information about the Beginners mailing list