At 10:50 AM +0900 2005/4/16, Kaoru Hosokawa wrote:
My solution based on Bernie's solution:

init :: [a] -> [a]
init xs = foldr (left (length xs)) xs xs

left :: Int -> a -> [a] -> [a]
left n x xs
     | n == length xs = []
     | otherwise      = x : xs

I use the foldr return value for the empty list as the input itself. Use this value as a marker to signify that I am at the end of the list by checking its length.

Using length causes this definition of init to fail on infinite lists.

--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
[EMAIL PROTECTED]                [EMAIL PROTECTED]
http://www.cs.utexas.edu/users/ham/richards
------------------------------------------------------------------
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to