[Haskell-cafe] Thompson's Exercise 9.13

Henning Thielemann lemming at henning-thielemann.de
Tue Apr 12 12:53:40 EDT 2005


On Tue, 12 Apr 2005, Hamilton Richards wrote:

> Here's a solution:
>
>>   init :: [a] -> [a]
>>   init xs = tail (foldr keep [] xs)
>>     where
>>     keep :: a -> [a] -> [a]
>>     keep x []  = [x]
>>     keep x [y] = [x,x]
>>     keep x (y:z:zs)  = x:x:y:zs

Nice idea!

One case can be eliminated.

> init :: [a] -> [a]
> init xs = tail (foldr keep [] xs)
>   where
>     keep :: a -> [a] -> [a]
>     keep x []  = [x]
>     keep x (_:zs)  = x:x:zs


More information about the Haskell-Cafe mailing list