[Haskell-cafe] efficient chop

Daniel Gorín dgorin at dc.uba.ar
Wed Sep 14 08:53:03 CEST 2011


On Sep 14, 2011, at 5:29 AM, Kazu Yamamoto (山本和彦) wrote:

> 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


More information about the Haskell-Cafe mailing list