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"

This is as far as I got:

        init xs = foldr left [] xs

        left :: a -> [a] -> [a]
        left x []       = []
        left x1 : (x2 : xs) = x1 : (left x2 xs)

But this returns [] and doesn't work. I can't get "left" to know that it is working with the rightmost element. It just throws away every element when its right hand side is empty.

I found a solution that works for Strings, but I am looking for a more general solution. This exercise may yet again be one of those that is difficult to solve with my current knowledge of Haskell, but I'm asking anyway.

--
Kaoru Hosokawa
[EMAIL PROTECTED]

_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to