
Hi, I am reading Real World Haskell and have some questions about the piece of code implementing foldl in terms of foldr: -- file: ch04/Fold.hs 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) The first argument to foldr is of type (a -> b -> a), which takes 2 arguments. But 'step' here is defined as a function taking 3 arguments. What am I missing here? (Partial application? The order of execution?) Btw, is there a way I can observe the type signature of 'step' in this code? Thanks in advance! -- Pan, Xingzhi http://www.panxingzhi.net

Xingzhi Pan wrote:
The first argument to foldr is of type (a -> b -> a), which takes 2 arguments. But 'step' here is defined as a function taking 3 arguments. What am I missing here?
You can think of step as a function of two arguments which returns a function with one argument (although in reality, as any curried function, 'step' is _one_ argument function anyway): step :: b -> (a -> c) -> (b -> c) e.g. 'step' could have been defined as such: step x g = \a -> g (f a x) to save on lambda 'a' was moved to argument list. -- View this message in context: http://old.nabble.com/foldl-in-terms-of-foldr-tp27322307p27324376.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.

On Tue, 26 Jan 2010, Xingzhi Pan wrote:
Hi,
I am reading Real World Haskell and have some questions about the piece of code implementing foldl in terms of foldr:
-- file: ch04/Fold.hs 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)
The first argument to foldr is of type (a -> b -> a), which takes 2 arguments. But 'step' here is defined as a function taking 3 arguments. What am I missing here? (Partial application? The order of execution?)
http://www.haskell.org/haskellwiki/Foldl_as_foldr
Btw, is there a way I can observe the type signature of 'step' in this code?
http://www.haskell.org/haskellwiki/Determining_the_type_of_an_expression

On Tue, Jan 26, 2010 at 5:04 AM, Xingzhi Pan
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

On Tue, Jan 26, 2010 at 1:56 PM, Ryan Ingram
On Tue, Jan 26, 2010 at 5:04 AM, Xingzhi Pan
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.
This is the only thing I use -XImplicitParams for: {--} :t \x g a -> g (?f a x) \x g a -> g (?f a x) :: (?f::t -> t1 -> t2) => t1 -> (t2 -> t3) -> t -> t3 Which differentiates the givens and gives them names :-) Luke
participants (5)
-
Eduard Sergeev
-
Henning Thielemann
-
Luke Palmer
-
Ryan Ingram
-
Xingzhi Pan