[Haskell-cafe] ANNOUNCE: The Monad.Reader - Issue 6

Matthew Brecknell haskell at brecknell.org
Wed Jan 31 21:20:45 EST 2007


> dw :: (a -> Bool) -> [a] -> [a]
> dw p = reverse . fst . foldl comb ([],False)
>   where comb (xs,done) x | done      = (x:xs, True)
>                          | p x       = (xs, False)
>                          | otherwise = (x:xs, True)
> 
> Which is the simplest working algorithm I could come up with; sadly it
> breaks the lazinesss constraint.

Speaking of the laziness constraint, the simplest solution to the
strictness of dwBackwards (solution 1) would be to use irrefutable
pattern matching in the combine function:

dwBackwards predicate = fst . foldr combine ([],[]) where
  -- Note the tilde in the next line...
  combine x ~(ys,xs)
    | predicate x = (ys, x:xs)
    | otherwise = (x:xs, x:xs)



More information about the Haskell-Cafe mailing list