2009/3/15 R J <rj248...@hotmail.com> > I need to write an implementation using foldl, and a separate > implementation using foldr, of a function, "remdups xs", that removes > adjacent duplicate items from the list xs. For example, remdups > [1,2,2,3,3,3,1,1]= [1,2,3,1]. > > My approach is first to write a direct recursion, as follows: > > remdups :: (Eq a) => [a] -> [a] > remdups [] = [] > remdups (x : []) = [x] > remdups (x : xx : xs) = if x == xx then remdups (x : xs) else x : > remdups (xx : xs) > > This code works, but it has three cases, not usual two, namely [] and (x : > xs). > > What, if any, is the implementation using only two cases? > > Also, if three cases are required, then how can it be implemented using > foldr, and how using foldl? > > Thanks. >
Perhaps it would be helpful to define a helper function with this signature: prepend :: (Eq a) => a -> [a] -> [a] Which for "prepend x xs" will put x at the front of the list, so long as the first element of the list xs is different from x. Once you have this function, take a look at the type signature for foldr. -- Sebastian Sylvan +44(0)7857-300802 UIN: 44640862
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe