openlang

3 + 4 * 2 is 14, and that is the easy part

Everyone new to Smalltalk meets 3 + 4 * 2 early and gets told it's 14, because binary messages go left to right with no arithmetic precedence at all. That part sticks after one demo (it really is just two sends, + to 3 and then * to whatever came back, and numbers get no special treatment, which people find either refreshing or mildly insulting).

What trips learners for much longer is the other half of the rule. Unary messages bind tighter than binary ones, and binary tighter than keyword ones. So 2 + 3 squared is 11, because squared goes to 3 before anything else happens, and 2 raisedTo: 3 + 1 is 16, because the keyword message waits for its argument to finish.

I'm tempted to teach the three levels first and use the arithmetic only as the example of the binary rule, instead of opening with arithmetic as the famous oddity. Not sure yet that it's better. It might stop people calling it a quirk, which would already be something.

后续讨论 1

顺着彼此的依据读下去
  1. 固定链接

    Rule first is the order I'd pick, with the rule stated as precisely as the examples. Three levels, unary then binary then keyword, and left to right within a level. Then each example tests one clause. 2 + 3 squared checks unary over binary. 3 + 4 * 2 checks left to right. 2 raisedTo: 3 + 1 checks binary over keyword. A learner who predicts all three has the rule, and one who misses tells you which clause is missing. SQL readers may be an easy audience, since a OR b AND c meaning a OR (b AND c) is a precedence table most of them have had to look up at least once.

同一个问题的其他角度

  1. Notation first, machine later

    APL was a notation before it was a program. Iverson used it on paper to describe algorithms, and the 1962 book A Programming…

    回复 1最近活动 apl
  2. Where Pharo code actually lives

    Poking at Pharo after a long time with Ruby, and I'm stuck on something embarrassingly basic for someone used to files. I mad…

    回复 1最近活动 smalltalk
  3. A useful review comment on a retain cycle names the owner

    “Just use weak everywhere” is a review comment the author can't really answer. The usual case is a child object holding a del…

    回复 1最近活动 swift