[Haskell-cafe] Maintaining laziness: another example

Eugene Kirpichov ekirpichov at gmail.com
Mon Jun 8 08:31:19 EDT 2009


Cool! Probably one should start teaching with 'case' instead of
pattern function definitions; that would put an accent on what is
forced and in what order. Only after the student understands the
laziness issues, introduce pattern signatures.

2009/6/8 Martijn van Steenbergen <martijn at van.steenbergen.nl>:
> Hello,
>
> While grading a Haskell student's work I ran into an example of a program
> not being lazy enough. Since it's such a basic and nice example I thought
> I'd share it with you:
>
> One part of the assignment was to define append :: [a] -> [a] -> [a],
> another to define cycle2 :: [a] -> [a]. This was her (the majority of the
> students in this course is female!) solution:
>
>> append :: [a] -> [a] -> [a]
>> append [] ys = ys
>> append xs [] = xs
>> append (x:xs) ys = x : (append xs ys)
>>
>> cycle2 :: [a] -> [a]
>> cycle2 [] = error "empty list"
>> cycle2 xs = append xs (cycle2 xs)
>
> This definition of append works fine on any non-bottom input (empty, finite,
> infinite), but due to the unnecessary second append case, cycle2 never
> produces any result.
>
> Martijn.
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>



-- 
Eugene Kirpichov
Web IR developer, market.yandex.ru


More information about the Haskell-Cafe mailing list