How to use the kit! in Schema functions?

How would you use set! in a simple procedure f such that the estimate (+ (f 0) (f 1)) will return 0 if the arguments in + are evaluated from left to right, but will return 1 if the arguments are evaluated from right to left?

+5
source share
2 answers

The simplest approach is probably to preserve some external state and the implementation of f affects its contents.

(define x 0)
(define (f n) (let ((tmp x)) (set! x n) tmp))

So x is initially 0, and each call to f returns the current value of x and saves the argument as a new value of x. Thus, (f 0) followed by (f 1), both return 0, leaving the final value of x equal to 1. When evaluating (f 1), followed by (f 0), there will be 0, and then 1, with finite x of 0.

+6

call/cc.

(define (f)
  (call/cc
    (lambda (c) (+ (c 0) (c 1)))))

(write (f))

c + f, 0 1, , .

, , , 0.

-1

All Articles