[Haskell-cafe] Thompson's Exercise 9.13
Kaoru Hosokawa
khosokawa at gmail.com
Sun Apr 10 02:44:12 EDT 2005
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
khosokawa at gmail.com
More information about the Haskell-Cafe
mailing list