Proposal: adding 'tailDropWhile' to Data.List

Kazu Yamamoto ( 山本和彦 ) kazu at iij.ad.jp
Wed Sep 28 11:19:30 CEST 2011


Hello,

Many languages provide the 'chop' or 'trip' function but Data.List
does not. I would like to add a new function called 'tailDropWhile'
so that people can easily implement 'chop' for String:

chop :: String -> String
chop = tailDropWhile isSpace

The definition of tailDropWhile is as follows:

{-
 wren ng thornton's version. This is lazier than Aoe's one.
 Out and inP implement push-down automata.
-}
tailDropWhile :: (a -> Bool) -> [a] -> [a]
tailDropWhile p = out
  where
    out []        = []
    out (x:xs)
      | p x       = inP [x] xs
      | otherwise = x : out xs
    inP _ []      = []
    inP ss (x:xs)
      | p x       = inP (x:ss) xs
      | otherwise = reverse ss ++ x : out xs

{- Mitsutoshi Aoe's version.
   This is faster is many cases but more strict.
   Just for reference.
tailDropWhile :: (a -> Bool) -> [a] -> [a]
tailDropWhile p = foldr go []
  where
    go x xs
      | p x && null xs = []
      | otherwise      = x:xs
-}

For more information, please read:
	http://www.mail-archive.com/haskell-cafe@haskell.org/msg93192.html

Discussion period: 2 weeks.

Regards,

--Kazu



More information about the Libraries mailing list