Reputation: 69
I would like to known how deterministic Racket's evaluation order is when set!
is employed. More specifically,
#%app
always evaluates its arguments from left to right?Take, for instance, this snippet:
#lang racket
(define a 0)
(define (++a) (set! a (add1 a)) a)
(list (++a) (++a)) ; => ?
Could the last expression evaluate to something different than '(1 2)
, such as '(1 1)
, '(2 2)
or '(2 1)
?
I failed to find a definite answer on http://docs.racket-lang.org/reference.
Upvotes: 0
Views: 185
Reputation: 48745
Unlike Scheme, Racket is guaranteed left to right. So for the example call:
(proc-expr arg-expr ...)
You can read the following in the Guide: (emphasis mine)
A function call is evaluated by first evaluating the proc-expr and all arg-exprs in order (left to right).
That means that this program:
(define a 0)
(define (++a) (set! a (add1 a)) a)
(list (++a) (++a))
; ==> (1 2)
And it is consistent. For Scheme (2 1)
is an alternative solution. You can force order by using bindings and can ensure the same result like this:
(let ((a1 (++ a)))
(list a1 (++ a)))
; ==> (1 2)
Upvotes: 2