Am Sonntag, 10. April 2005 08:44 schrieb Kaoru Hosokawa:
> 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)

Try 

left :: (Bool, [a]) -> (Bool, [a]),

res :: (Bool, [a]) -> [a],

init' = res . foldr left (False, [])

for example. It's not too difficult then.

>
> 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.
>
I cannot imagine how you could do it for Strings, but not for general lists.
Mind sending me the code?

Hope this helps,

Daniel

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

Reply via email to