[email protected]
[Top] [All Lists]

Re: [Haskell-cafe] foldl in terms of foldr

Subject: Re: [Haskell-cafe] foldl in terms of foldr
From: Ryan Ingram
Date: Tue, 26 Jan 2010 12:56:49 -0800
On Tue, Jan 26, 2010 at 5:04 AM, Xingzhi Pan <[email protected]> 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
_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

<Prev in Thread] Current Thread [Next in Thread>