[Haskell-cafe] foldl in terms of foldr

Ryan Ingram ryani.spam at gmail.com
Tue Jan 26 15:56:49 EST 2010


On Tue, Jan 26, 2010 at 5:04 AM, Xingzhi Pan <vengeance.storm at gmail.com> wrote:
> myFoldl :: (a -> b -> a) -> a -> [b] -> a
> myFoldl f z xs = foldr step id xs z
>    where step x g a = g (f a x)
>
> Btw, is there a way I can observe the type signature of 'step' in this code?

Here is how I do it:

Prelude> :t \[f] -> \x g a -> g (f a x)
\[f] -> \x g a -> g (f a x)
  :: [t1 -> t -> t2] -> t -> (t2 -> t3) -> t1 -> t3

The [] are unnecessary but help me differentiate the "givens" from the
actual arguments to the function.

Here's a way that gives better type variables and properly constrains
the type of f:

Prelude> let test [f] x g a = g (f a x) where typeF = f `asTypeOf` const
Prelude> :t test
test :: [a -> b -> a] -> b -> (a -> t) -> a -> t

  -- ryan


More information about the Haskell-Cafe mailing list