[Haskell-cafe] Thompson's Exercise 9.13

Hamilton Richards ham at cs.utexas.edu
Fri Apr 15 23:57:12 EDT 2005

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.

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

More information about the Haskell-Cafe mailing list