Define a one-parameter procedure prev that returns as its value the argument that was passed to it the previous time it was called. The first time prev is called it should return the symbol first-call. (prev ’a) ’first-call (prev 3) ’a (prev (+ 1 5)) 3
时间: 2024-02-22 10:00:12 浏览: 134
Here is a possible implementation of the `prev` procedure in Scheme:
```scheme
(define (prev)
(let ((prev-value 'first-call))
(lambda (x)
(let ((result prev-value))
(set! prev-value x)
result))))
```
The `prev` procedure returns a new anonymous procedure that takes one argument `x`. The first time the anonymous procedure is called, it returns the symbol `first-call`, which is the initial value of the `prev-value` variable. On subsequent calls, the anonymous procedure returns the previous value of `x` that was passed to it, which is stored in `prev-value`. The `set!` special form is used to update the value of `prev-value` to the current value of `x`. Here are some examples of using `prev`:
```scheme
(define p (prev))
(p 'a) ;=> first-call
(p 3) ;=> a
(p (+ 1 5)) ;=> 3
```
Note that the `prev` procedure has no parameters, as requested in the problem statement. The parameter `x` is passed to the anonymous procedure returned by `prev`.
阅读全文