[Haskell-cafe] Dropping trailing nulls from a list of list

Udo Stenzel u.stenzel at web.de
Wed Mar 8 14:07:24 EST 2006


Jeff.Harper at handheld.com wrote:
> 
> Today, I reviewed a function I wrote a few months ago.  The function,
> dropTrailNulls, takes a list of lists and drops trailing null lists.  For
> instance:
> 
> *Main> dropTrailNulls [[1],[2,3],[],[]]
> [[1],[2,3]]

dropTrailNulls = foldr dtn []
  where
    dtn [] [] = []
    dtn  x xs = x:xs
    
 
> dropTrailNulls list = reverse (dropWhile null (reverse list))

As the other responses said, this is needlessly strict.  Work on
deforesting reverse exists, but you can't count on it happenig.


> is there a more efficient idiom for addressing these problems?

Well, there's always the basic fold.  I'm not sure there's any lesson to
be learnt here other than "fold is your friend".


Udo.
-- 
F:	Was ist ansteckend und kommutiert?
A:	Eine Abelsche Grippe.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060308/5802fb34/attachment.bin


More information about the Haskell-Cafe mailing list