> 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
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe

Reply via email to