The syntax of set!
is
(set! <identifier> <expression>)
which is to say that the first form must be a symbol. In your code you are using:
(cdr (car main-list))
as the 'identifier' - hence the error.
Perhaps your background is CommonLisp and you are expecting set!
to behave like setf
? In Scheme there are separate functions for setting the 'car' and 'cdr' of a pair. Use set-car!
and set-cdr!
> (define pair (cons 'car 'cdr))
> pair
(car . cdr)
> (set-car! pair 'foo)
> pair
(foo . cdr)
> (set-cdr! pair 'bar)
> pair
(foo . bar)
In R6RS (and probably R7RS) set-car!
and set-cdr!
can be found in the (rnrs mutable-pairs)
library