The body of all derived forms from lambda
is evaluated from left to right in order as in a begin
. All but the last expression are only evaluated for side effect while the result of the very last expression will be the result of your function:
((lambda (x)
(+ x 1) ; not in tail position. no effect so it's dead code
2) ; the evaluation of 2 will be the result every time
3) ; argument, only used by dead code.
It actually does produce the sum, then throw it away to evaluate the last expression. Only good use for dead code is keeping warm in the winter. An example of a more sensible use of more expressions in the body:
(define (hypopotemus a b)
(define (square x) ; Not in tail position, creates
(* x x)) ; a function as side effect.
(sqrt (+ (square a) (square b)))) ; Tail expression calculates the result
And since this mentions evaluation order. While arguments to functions are evaluated strictly left to right in #lang racket
in all the the Scheme reports, like #!r6rs
, an implementation (like racket) can choose any order. eg.
((lambda (a b c) (display "d"))
(display "a")
(display "b")
(display "c")
While the above code always prints "abcd" in #lang racket
it is only one of
6 possible outcomes in Scheme since you don't know which order the arguments are evaluated first, middle and last and the printing will happen in evaluating order. I know racket of course evaluates their Scheme code left to right while Ikarus does it ion the opposite order.