I am defining a function replaceOccurrence that replaces the first occurrence of either of two symbols. I don't understand why list variable is not bound Chez Scheme Version 9.5
(define replaceOcurrence
(λ (new ocurrence1 ocurrence2 lista)
(cond
((null? lista) '())
(else (cond
((eq? ocurrence1 (car lista))
(cons new (cdr (lista))))
((eq? ocurrence2 (car lista))
(cons new (cdr (lista))))
(else (cons (car lista)
(replaceOcurrence new ocurrence1 ocurrence2 (cdr lista)))))))))
Exception: variable lista is not bound
Chez Scheme does not support λ
as a replacement for lambda
. Racket does, and says so explicitly in the documentation. Guile appears to support λ
too, but I don't see that documented. Since λ
is not recognized as a special form in Chez Scheme, the arguments to λ
are evaluated first, and since they have not yet been bound, the reported error is issued.
There is another problem in the OP code: lista
is alone in parentheses twice; this attempts to call lista
as a procedure each time. Also, occurrence is spelled with two Cs; and prefer kebab-case
to camelCase
in Scheme. Here is the fixed code:
(define replace-occurrence
(lambda (new occurrence1 occurrence2 lista)
(cond
((null? lista) '())
(else
(cond
((eq? occurrence1 (car lista))
(cons new (cdr lista)))
((eq? occurrence2 (car lista))
(cons new (cdr lista)))
(else
(cons (car lista)
(replace-occurrence new occurrence1 occurrence2 (cdr lista)))))))))
The best, and most portable, way to solve OP problem is to simply use lambda
. But, if one really wants to use λ
, a macro can be used to provide the necessary syntax:
(define-syntax λ
(syntax-rules ()
[(_ formals . body)
(lambda formals . body)]))
With the inclusion of the above macro (and the other necessary changes mentioned in the first part of the answer), OP code can use λ
in place of lambda
.