
My solution based on Bernie's solution: init :: [a] -> [a] init xs = foldr (left (length xs)) xs xs left :: Int -> a -> [a] -> [a] left n x xs | n == length xs = [] | otherwise = x : xs I use the foldr return value for the empty list as the input itself. Use this value as a marker to signify that I am at the end of the list by checking its length. On 2005/04/10, at 17:13, Bernard Pope wrote:
On Sun, 2005-04-10 at 15:44 +0900, Kaoru Hosokawa wrote:
I've been working through Thompson's exercises and got to one I could not solve. It's Exercise 9.13. This is where I need to define init using foldr.
init :: [a] -> [a] init "Greggery Peccary" ~> "Greggary Peccar"
Hi,
Here's a tentative solution:
myinit xs = foldr (f (length xs)) [] xs where f len next [] | len == 1 = [] | otherwise = [next] f len next list@(x:xs) | length list + 1 == len = next : xs | otherwise = x : next : xs
-- Kaoru Hosokawa khosokawa@gmail.com