Why this order of parameters

Hello all, there are two functions, where the order of parameters confuses me. Ons is runState :: State s a -> s -> (a, s) I understand that in the constructor s has to be first, so we can turn (State s) into a monad. But why doesn't s come first in the result too, as in runState :: State s a -> s -> (s, a) The other example is foldl, foldr foldl :: (a -> b -> a) -> a -> [b] -> a foldr :: (a -> b -> b) -> b -> [a] -> b For once the list is a list of as in foldl, but a list of as in foldr. Now that can be fixed with renaming the type parameters foldr :: (b -> a -> a) -> a -> [b] -> a this is the exact same thing and resembles more the type of foldl. But still the function argument has its parameters flipped. foldl's function takes the list element as second parameter and foldr's takes it as first parameter. Why is that so?

Hi, my first intuition about this is that in data constructor it technically doesn't matter, but you could argue that "a" represents the actual result of the function so it comes first. Second comes the state, which is the side thing, hence the secondary/less important position. As for the order of type constructor parameters you are right - state is part of the structure that Monoid, Functor, Applicative, Monad and the like use. Martin martin:
runState :: State s a -> s -> (a, s)
I understand that in the constructor s has to be first, so we can turn (State s) into a monad. But why doesn't s come first in the result too, as in
runState :: State s a -> s -> (s, a)

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr :: (b -> a -> a) -> a -> [b] -> a
are exactly the same types; simply inverted names for the type variables.
I don't clearly see any benefit.
Alex.
On Nov 12, 2015 12:00 PM, "Martin Vlk"
Hi, my first intuition about this is that in data constructor it technically doesn't matter, but you could argue that "a" represents the actual result of the function so it comes first. Second comes the state, which is the side thing, hence the secondary/less important position.
As for the order of type constructor parameters you are right - state is part of the structure that Monoid, Functor, Applicative, Monad and the like use.
Martin
martin:
runState :: State s a -> s -> (a, s)
I understand that in the constructor s has to be first, so we can turn (State s) into a monad. But why doesn't s come first in the result too, as in
runState :: State s a -> s -> (s, a)
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

He means why is the first argument a -> b -> b in one function but b -> a
-> b in the other. And I have no answer for that. I admit to having been
caught by that in the past. You can't just replace one with the other in
code, you have to do a flip on the operator.
On Thu, Nov 12, 2015 at 12:34 PM, Alex Belanger
foldr :: (a -> b -> b) -> b -> [a] -> b foldr :: (b -> a -> a) -> a -> [b] -> a
are exactly the same types; simply inverted names for the type variables.
I don't clearly see any benefit.
Alex. On Nov 12, 2015 12:00 PM, "Martin Vlk"
wrote: Hi, my first intuition about this is that in data constructor it technically doesn't matter, but you could argue that "a" represents the actual result of the function so it comes first. Second comes the state, which is the side thing, hence the secondary/less important position.
As for the order of type constructor parameters you are right - state is part of the structure that Monoid, Functor, Applicative, Monad and the like use.
Martin
martin:
runState :: State s a -> s -> (a, s)
I understand that in the constructor s has to be first, so we can turn (State s) into a monad. But why doesn't s come first in the result too, as in
runState :: State s a -> s -> (s, a)
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

I think again this technically doesn't matter... I'm curious if this could be for the sake of making the types of the two functions (foldr/foldl) different? E.g. so that you can tell from the type what function it is. (I'm a Haskell beginner myself so would be curious to hear an expert's voice on this!) Martin martin:
foldl :: (a -> b -> a) -> a -> [b] -> a foldr :: (a -> b -> b) -> b -> [a] -> b
For once the list is a list of as in foldl, but a list of as in foldr. Now that can be fixed with renaming the type parameters
foldr :: (b -> a -> a) -> a -> [b] -> a
this is the exact same thing and resembles more the type of foldl. But still the function argument has its parameters flipped. foldl's function takes the list element as second parameter and foldr's takes it as first parameter.
Why is that so?

Hmmm, I seem to have missed part of this conversation, but I’ll toss in my two cents anyhow:
Consider the identities
foldl (•) s [x,y,z] == ((s•x)•y)•z
foldr (•) s [x,y,z] == x•(y•(z•s))
and I think it should be clear that the type of (•) must be a→b→a in the former and b→a→a in the latter.
Either of these identities would look rather odd when written in operator notation if (•) were flipped!
– Harald
-----Original Message-----
From: Martin Vlk
I think again this technically doesn't matter...
I'm curious if this could be for the sake of making the types of the two functions (foldr/foldl) different? E.g. so that you can tell from the type what function it is.
(I'm a Haskell beginner myself so would be curious to hear an expert's voice on this!)
Martin
martin:
foldl :: (a -> b -> a) -> a -> [b] -> a foldr :: (a -> b -> b) -> b -> [a] -> b
For once the list is a list of as in foldl, but a list of as in foldr. Now that can be fixed with renaming the type parameters
foldr :: (b -> a -> a) -> a -> [b] -> a
this is the exact same thing and resembles more the type of foldl. But still the function argument has its parameters flipped. foldl's function takes the list element as second parameter and foldr's takes it as first parameter.
Why is that so?
Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Harald Hanche-Olsen Førsteamanuensis Institutt for matematiske fag Norges teknisk-naturvitenskapelige universitet 7491 Trondheim http://www.math.ntnu.no/~hanche/

On Fri, Nov 13, 2015 at 12:49 AM, Martin Vlk
I'm curious if this could be for the sake of making the types of the two functions (foldr/foldl) different? E.g. so that you can tell from the type what function it is.
No, that would be foolish. One doesn't simply make the types different to distinguish them. Suppose the elements are already naturally foldable. Then foldr and foldl have the same type signature: foldl, foldr :: (a -> a -> a) -> a -> [a] -> a But sometimes they are only foldable qua another type. Call it r. If you work through Harald's reasoning, you'll arrive at: foldr :: (a -> r -> r) -> r -> [a] -> r foldl :: (r -> a -> r) -> r -> [a] -> r -- Kim-Ee

Harald and Kim-Ee, thanks for your explanations. So in other words the order of parameters naturally follows from the corresponding equations and is not driven by technical need. A follow up question.. what do you mean by elements being "naturally foldable"? Is it that you could just cons them together without any transformation? Martin Kim-Ee Yeoh:
On Fri, Nov 13, 2015 at 12:49 AM, Martin Vlk
wrote: I'm curious if this could be for the sake of making the types of the two functions (foldr/foldl) different? E.g. so that you can tell from the type what function it is.
No, that would be foolish. One doesn't simply make the types different to distinguish them.
Suppose the elements are already naturally foldable. Then foldr and foldl have the same type signature:
foldl, foldr :: (a -> a -> a) -> a -> [a] -> a
But sometimes they are only foldable qua another type. Call it r. If you work through Harald's reasoning, you'll arrive at:
foldr :: (a -> r -> r) -> r -> [a] -> r foldl :: (r -> a -> r) -> r -> [a] -> r
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

On Sun, Nov 15, 2015 at 2:37 AM, Martin Vlk
A follow up question.. what do you mean by elements being "naturally foldable"? Is it that you could just cons them together without any transformation?
E.g. Numbers are naturally summable. Summing is a form of folding. -- Kim-Ee
participants (6)
-
Alex Belanger
-
David McBride
-
Harald Hanche-Olsen
-
Kim-Ee Yeoh
-
martin
-
Martin Vlk