Common Lisp to Scheme translation
Help for translating the book's code to Scheme
Table of Contents
1 Common Lisp to Scheme translation
1.1 Straight forward translations
defun
->define
defvar
->define
orlet
setf
->define
orlet
function
or#'
-> nothing, no difference is made in referring to values or procedures, Scheme is a Lisp 1.mapcar
->map
funcall
-> simply call the procedure, s-expr with procedure in the first place
1.2 Others
1.2.1 NIL
In Common Lisp NIL
is a defined symbol:
(define NIL '())
1.2.2 car
car
of an empty list in Common Lisp returns NIL, the empty list again. car
in Scheme does not return the empty list, but raises an error. This means, that, for compatibility reasons, we have to write a little procedure, which acts like car
from Common Lisp:
(define NIL '()) (define car/nil (lambda (possibly-empty-list) "In Common Lisp car of an empty list returns the empty list called NIL. In Scheme it would be an error to call car on an empty list. We write a wrapper, to avoid an error." (cond [(null? possibly-empty-list) NIL] [else (car possibly-empty-list)])))
1.2.3 rest
rest
in Common Lisp also returns NIL
, when called with the empty list as argument, so a wrapper is needed:
(define NIL '()) (define rest (lambda (lst) (cond [(null? lst) NIL] [else (cdr lst)])))
1.2.4 defparameter
A parameter is explained in the book as a binding, that usually does not chance, a constant. A change to it is explained as a change to the program, not a change by the program, in contrast to using defvar
.
Since in Scheme we usually try to avoid using assignment or set!
anyway, we can get away with following a convention of naming constants with enclosing asterisks: *constant*
and using define
.
1.2.5 case
???