Advertisement

How to distinguish between commutativity and associativity

How to distinguish between commutativity and associativity You confuse them a lot, and it's not your fault. They are similar in that they are both about order. Associativity is about the order of operations, and commutativity is about the order of arguments. We go through some examples to help clear up the distinction.

►► Audio, Video, and Transcript available:
►► Subscribe on iTunes:

Transcript

[laughs] Folks, this is the big one. How do you distinguish between commutativity and associativity? How do you tell the difference?

My name is Eric Normand, and I help people thrive with functional programming.

This is frustrating for me because I get these two confused all the time, even though I know the difference. I know I do.

I could write out the difference in math, but still, when I'm talking and when I just casually going through a problem, I make mistakes and it sucks. I think one of the problems is they are very closely associated, especially the way they're taught.

The examples that are given, which are usually addition and multiplication, don't distinguish between associativity and commutativity very well. Both addition and multiplication are both associative and commutative. If you use them as examples, you can't figure out from the examples which one is which.

In fact, it's hard if you find a function that's commutative. It's actually a little hard to find a commutative function that's not associative. You can find them. You can construct them. They're not that hard. It seems most of the simple, well-known mathematical functions that are commutative are also associative.

We're going to find some. We're going to talk about them in this episode. Let's get started.

I'm assuming that if you're listening to this, you understand some of the basics of doing math with parentheses. When you need parentheses, the order of operations, specifically in multiplication and addition, and the idea of arguments to your operators.

If we just have those basic ideas, we can clarify all of this.

One of the troubles is that both associativity and commutativity are about orders. I like to say commutativity is about order and associativity is about grouping. Maybe that just muddles it more because if you read on Wikipedia, they say associativity is about order of operations. They're talking about order again.

At least for this episode, I'm going to say that they're both about order. They're both about different orders, types of orders. Because when we write out a mathematical formula, x + y * z * w, then we've got different kinds of orders there.

First is the lexical order of everything written out. We needed to have some kind of linear order because that's how our paper works.

Then there's also a structural order to it. There's a nesting structure. That nesting is implicit when we write it out like that because we have this idea of order of operations. You're going to do the multiplications before you do the additions. We learn this in school.

If you're going to break that rule -- you need to do an addition first before a multiplication -- you put a parentheses around it. You put a pair of parentheses. That indicates, "Do this one before..." Break the rule for this one case. You're going to use these parentheses.

There's two orders. There's the lexical written order. Then there's the structural order that you're supposed to execute the computation in. That's the difference between the two orders. Associativity says the order of operations does not matter.

If you look at a + b + c + d, the order of operations does not matter. They're all plus. You could essentially draw parentheses around any way you want. You could start from the left, work a + b, then add c, and then add d. You start from the right. You could start with the d, then add the c, and then add the b. You could do that. You get the same answer.

That's what associativity means -- that you could group it however you want. You could put the parentheses here, you could the parentheses here, you'd have the same answer.

This works because they're all plus. They're all the same operation. Once you throw times in there, it's not going to work anymore. You're going to have to obey the order of operations. When you've just got pluses, you can do that.

We could also call this grouping because that's what parentheses do. They group the operations with their operand. They group these expressions together. They tell you this one needs to stay together. This goes first. This one goes before everything else. They're indicating the order of operations, the order that you execute the expression in.

Commutativity is the order of arguments. This is the lexical order.

functional programming,software engineering,clojure,java,javascript,haskell,functional programming tutorial,functional programming languages,functional programming course,theory of functional programming,functional programming advantages,functional programming benefits,

Post a Comment

0 Comments