
Hello, My friend reached the following version: chop :: String -> String chop = foldr go [] where go x xs | isSpace x && null xs = [] | otherwise = x:xs This version is faster than the reverse version in most cases. The point is checking "isSpace" first and falling into "otherwise" in many cases, which is a natural co-recursion. Thanks anyway. --Kazu
Hello,
Of course, I use ByteString or Text for real programming. But I would like to know whether or not there are any efficient methods to remove a tail part of a list.
--Kazu
In that case, I would prefer this version, since it is lazier:
lazyChop :: String -> String lazyChop s = pref ++ if null s' then [] else (mid_sp ++ lazyChop s') where (pref,sp_suf) = break isSpace s (mid_sp,s') = span isSpace sp_suf
By "lazier" I mean:
*Main> chopReverse $ "hello world " ++ undefined "*** Exception: Prelude.undefined *Main> chopFoldr $ "hello world " ++ undefined "*** Exception: Prelude.undefined *Main> lazyChop $ "hello world " ++ undefined "hello world*** Exception: Prelude.undefined
Daniel