The dlist package contains the DList
data type, which has lots of instances, but not Foldable
or Traversable
. In my mind, these are two of the most "list-like" type classes. Is there a performance reason that DList
is not an instance of these classes?
Also, the package does implement foldr
and unfoldr
, but none of the other folding functions.
Haskell, in turn, has two fundamental functions representing reducing, or, as we call it, folding – foldl and foldr – that differ in the order of the folding. foldl reduces elements of a container from left to right (as reduce in other languages usually does), while foldr reduces from right to left.
However, a fold doesn't have to return a single value. It can return a collection, such as another list, as well. The fact that folds are higher-order functions are very important, because the reason why have higher order functions at all is to take a common programming pattern and encapsulate it in a function.
Difference Between foldl and foldr The difference is that foldl is tail-recursive, whereas foldr is not. With foldr and non-optimized patterns, proc is applied to the current value and the result of recursing on the rest of the list. That is, evaluation cannot complete until the entire list has been traversed.
Haskell : foldr. Description: it takes the second argument and the last item of the list and applies the function, then it takes the penultimate item from the end and the result, and so on. See scanr for intermediate results.
One alternative you should consider instead of DList
is to use Church-encoded lists. The idea is that you represent a list as an opaque value that knows how to execute a foldr
over a list. This requires using the RankNTypes
extension:
{-# LANGUAGE RankNTypes #-}
import Prelude
import Control.Applicative
import Data.Foldable (Foldable)
import qualified Data.Foldable as F
import Data.Traversable (Traversable)
import qualified Data.Traversable as T
-- | Laws:
--
-- > runList xs cons nil == xs
-- > runList (fromList xs) f z == foldr f z xs
-- > foldr f z (toList xs) == runList xs f z
newtype ChurchList a =
ChurchList { runList :: forall r. (a -> r -> r) -> r -> r }
-- | Make a 'ChurchList' out of a regular list.
fromList :: [a] -> ChurchList a
fromList xs = ChurchList $ \k z -> foldr k z xs
-- | Turn a 'ChurchList' into a regular list.
toList :: ChurchList a -> [a]
toList xs = runList xs (:) []
-- | We can construct an empty 'ChurchList' without using a @[]@.
nil :: ChurchList a
nil = ChurchList $ \_ z -> z
-- | The 'ChurchList' counterpart to '(:)'. Unlike 'DList', whose
-- implementation uses the regular list type, 'ChurchList' doesn't
-- rely on it at all.
cons :: a -> ChurchList a -> ChurchList a
cons x xs = ChurchList $ \k z -> k x (runList xs k z)
-- | Append two 'ChurchList's. This runs in O(1) time. Note that
-- there is no need to materialize the lists as @[a]@.
append :: ChurchList a -> ChurchList a -> ChurchList a
append xs ys = ChurchList $ \k z -> runList xs k (runList ys k z)
-- | Map over a 'ChurchList'. No need to materialize the list.
instance Functor ChurchList where
fmap f xs = ChurchList $ \k z -> runList xs (\x xs' -> k (f x) xs') z
-- | The 'Foldable' instance is trivial, given the 'ChurchList' law.
instance Foldable ChurchList where
foldr f z xs = runList xs f z
instance Traversable ChurchList where
traverse f xs = runList xs step (pure nil)
where step x rest = cons <$> f x <*> rest
The downside to this is that there is no efficient tail
operation for a ChurchList
—folding a ChurchList
is cheap, but taking repeated tails is costly...
DList a
is a newtype wrapper around [a] -> [a]
, which has an a
in a contravariant position, so it cannot implement Foldable
or Traversable
, or even Functor
directly. The only way to implement them is to convert to and from regular lists (see the foldr
implementation), which defeats the performance advantage of difference lists.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With