[Haskell-beginners] a simple problem

Brent Yorgey byorgey at seas.upenn.edu
Thu Nov 11 12:02:42 EST 2010


On Thu, Nov 11, 2010 at 06:59:01PM +0200, Markus Läll wrote:
> Hi Anonymous W!
> 
> As list is a recursive datastructure, you need a recursive (or
> folding) function to check for duplicates. Right now your function
> really does accept only two-element lists.
> 
> The function would look something like this:
> 
> maybeA list@ ((a1,b1) : (a2,b2) : rest) =
>    if hasUniqueKeys list
>       then Just (a1, b1, b2)
>       else Nothing
>    where
>       hasUniqueKeys xs = ... check the list ...
> 
> You also need to decide what to do, if the list is empty or contains
> only one pair.

This is good advice.  I would add, however, that I don't think
hasUniqueKeys ought to be (directly) recursive.  Instead, use
functions from the standard libraries to manipulate the list and
decide whether it has unique keys.  Functions which may be helpful in
this task include map, fst, sort, group, length, any.

-Brent


More information about the Beginners mailing list