[Haskell-cafe] Thompson's Exercise 9.13
Hamilton Richards
ham at cs.utexas.edu
Thu Apr 14 17:57:16 EDT 2005
At 6:53 PM +0200 2005/4/12, Henning Thielemann wrote:
>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
Yes, much nicer!
Thanks,
--Ham
--
------------------------------------------------------------------
Hamilton Richards, PhD Department of Computer Sciences
Senior Lecturer The University of Texas at Austin
512-471-9525 1 University Station C0500
Taylor Hall 5.138 Austin, Texas 78712-0233
ham at cs.utexas.edu hrichrds at swbell.net
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
More information about the Haskell-Cafe
mailing list