[Haskell-cafe] Maintaining laziness: another example

Raynor Vliegendhart shinnonoir at gmail.com
Mon Jun 8 09:48:18 EDT 2009


This might be slightly related. When I was assisting a Haskell lab
course, I encountered solutions like the following:

> removeRoot :: BSTree a -> BSTree a
> removeRoot (Node x Empty Empty) = Empty
> removeRoot (Node x left  Empty) = left
> removeRoot (Node x Empty right) = right
> removeRoot (Node x left  right) = undefined {- not needed for discussion -}

The first removeRoot case is unnecessary. Students new to Haskell (or
maybe new to recursion in general?) seem to consider more base cases
than needed.


-Raynor


On 6/8/09, Martijn van Steenbergen <martijn at van.steenbergen.nl> wrote:
> 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
>


More information about the Haskell-Cafe mailing list