This was a recent question on StackOverflow: http://stackoverflow.com/questions/6270324/in-haskell-how-do-you-trim-whitespace-from-the-beginning-and-end-of-a-string/6270382#6270382
Where I started: If you have serious text processing needs then use the text package from hackage. And concluded: A quick Criterion benchmark tells me that (for a particularly long string of words with spaces and ~200 pre and post spaces) my trim takes 1.6 ms, the trim using reverse takes 3.5ms, and Data.Text.strip takes 0.0016 ms. Cheers, Thomas On Tue, Sep 13, 2011 at 8:03 PM, Kazu Yamamoto <[email protected]> wrote: > Hello Cafe, > > I would like to have an efficient implementation of the chop function. > As you guess, the chop function drops spaces in the tail of a list. > > chop " foo bar baz " > -> " foo bar baz" > > A naive implementation is as follows: > > chopReverse :: String -> String > chopReverse = reverse . dropWhile isSpace . reverse > > But this is not elegant. foldr version is as follows: > > chopFoldr :: String -> String > chopFoldr = foldr f [] > where > f c [] > | isSpace c = [] > | otherwise = c:[] > f c cs = c:cs > > But this code is slower than chopReverse in some cases. > > Are there any more efficient implementations of chop? Any suggestions? > > --Kazu > > _______________________________________________ > Haskell-Cafe mailing list > [email protected] > http://www.haskell.org/mailman/listinfo/haskell-cafe > _______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
