[Haskell-cafe] function unique

Brent Yorgey byorgey at gmail.com
Wed Jul 11 16:06:07 EDT 2007


>
> > I don't think this is possible.  Perhaps you misread the original
> problem
> > description?  The unique function is supposed to return a list of those
> > elements which occur exactly once in the input list, which is impossible
> to
> > determine for an infinite input list (the only way to prove that a given
> > element occurs only once in a list, in the absence of any other
> information,
> > is to examine every element of the list).  Of course, a function that
> > behaves like the unix utility "uniq" ( i.e. returning only one copy of
> every
> > list element) is possible to implement lazily in the manner you
> describe.
>
> Why wouldn't this work?  (I haven't tested it, sorry)
>
> unique = unique' []
>
> unique' _ [] = []
> unique' history (x:xs) = if x `elem` history
>   then next
>   else (x:next) where next = (uniq' (x:hist) xs)


Again, this is solving a different problem than  what the OP stated. Using
your definition:

 Prelude> :l unique
[1 of 1] Compiling Main             ( unique.hs, interpreted )
Ok, modules loaded: Main.
*Main> unique [1,2,3,1]
[1,2,3]
*Main>

...which behaves like the Unix utility 'uniq'.  But the problem described
originally is to write a function which produces [2,3] when given the same
input; 1 is not included in the output since it occurs more than once.

-Brent
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070711/565fe722/attachment.htm


More information about the Haskell-Cafe mailing list