Proposal: adding 'tailDropWhile' to Data.List
Ivan Lazar Miljenovic
ivan.miljenovic at gmail.com
Wed Sep 28 11:35:43 CEST 2011
On 28 September 2011 19:19, Kazu Yamamoto <kazu at iij.ad.jp> wrote:
> 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:
I don't think that "chop" is a very good name for this kind of
function; by the name, I would think more a function of type Int ->
[a] -> [[a]] that "chops up" a list into sub-lists of size n. "strip"
(which I presume is what you meant as your second suggestion) is a bit
better...
That said, I think there's a good reason not to add this: if you're
doing stuff at the end of a list, then you're probably doing something
wrong.
For this kind of textual task, you _really_ should be using text
anyway, and this function is already implemented for you:
http://hackage.haskell.org/packages/archive/text/0.11.1.5/doc/html/Data-Text-Lazy.html#v:stripEnd
>
> 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.
-1
--
Ivan Lazar Miljenovic
Ivan.Miljenovic at gmail.com
IvanMiljenovic.wordpress.com
More information about the Libraries
mailing list