Do operators in the Scheme programming language have associativity. For example, the code (- 5 2 1) evaluates to 2, but it could evaluate to 4 if it was evaluated right-associative. However, it is unambiguous if you were to write the expression as (- (- 5 2) 1). The first example, however, leads me to believe that Scheme is left-associative in the case that you do not group all operations into only two arguments at a time.

  • $\begingroup$ Uh... subtraction is not an associative operation in math, hence in programming. Your example has nothing to do with associativity, either. What is your actual question? $\endgroup$
    – chi
    Feb 20 '16 at 22:54
  • 1
    $\begingroup$ I mean left-associative vs. right-associative. Ex in regular algebra: 5-2-1 is 2 if evaluated left-associative. It is 4 if evaluated right-associative. $\endgroup$ Feb 20 '16 at 22:56
  • $\begingroup$ Right, but there's only one subtraction in your example, so it's not a matter of which subtraction to do first. Further, left/right-associativity make sense for infix operators, but in Scheme everything is prefix, so it requires explicit parentheses and no associativity rule is needed. E.g. (- (- 5 2) 1) vs (- 5 (- 2 1)) are already unambiguous, unlike 5-2-1. $\endgroup$
    – chi
    Feb 20 '16 at 23:26
  • $\begingroup$ Your comment seems like an answer to my question. I will rephrase my question $\endgroup$ Feb 21 '16 at 0:00
  • $\begingroup$ In scheme, (f u v w x y z) will try to evaluate f with the arguments u v w x y z, in that order. It does not evaluate (f u v) first and then something else with the rest. $\endgroup$
    – Pål GD
    Feb 28 '16 at 9:59

Scheme computes (- a b c) as a - b - c, i.e., strictly left to right.

It really makes no sense to talk about associativity in Scheme, which applies to how an otherwise ambiguous expression like a # b # c is to be interpreted, either as (a # b) # c or a # (b # c) (or even forbid such combinations). In Scheme no such ambiguity is possible, everything is prefix. And the function called is responsible to decide what to do with it's arguments, even if the number of arguments is variable (as in this case). It is probably more natural to work left-to-right.

  • $\begingroup$ (x . y) is an infix operator (even though only for the reader), and associativity concerns apply. $\endgroup$
    – wvxvw
    Feb 21 '16 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.