Wizard
Wizard

Reputation: 22113

scheme which support set-car! and set-cdr! in sicp

I am reading SICP 4.1.3 Evaluator Data Structures

(define (make-frame variables values)
  (cons variables values))
(define (frame-variables frame) (car frame))
(define (frame-values frame) (cdr frame))
(define (add-binding-to-frame! var val frame)
  (set-car! frame (cons var (car frame)))
  (set-cdr! frame (cons val (cdr frame))))

However, the set-car! is reported as unbounded in racket.

Then tried implements of 'GNU Guile 2.2.6', "GNU mit-scheme 10.1.10", unfortunately, set-car! are all not bound.

Search through the whole book of original version:

find . -type f -iname "*.org"  -exec grep --color -nH --null -e "set-car!" \{\} + |wc -l
27 

There are 27 occurrences through chapter 3 4 and 5,
and within chapter 3 I change setcar! to setcar of elisp. but elisp is not a good option for chapter 4 of metalinguistic abstraction.

The chapter 4 is bits of difficult thus now risk of adventure.

I find the good solution set-car!, set-cdr! unbound in racket?

(require rnrs/mutable-pairs-6)

As a solution, If replace set-car! set-cdr! with set-mcar! set-mcdr, the codes of chapter 4 and 5 will run smoothly by racket implementation?

Upvotes: 3

Views: 925

Answers (1)

soegaard
soegaard

Reputation: 31145

Bear in mind that Racket supports several languages. In #lang racket pairs are immutable, which means that the mutators set-car! and set-cdr! aren't present. If you want to use #lang racket you can work with list of boxes. A box can hold one value and you can use set-box! to change the value held by the box. Alternatively you use mutable pairs (and mutable lists) built with mcons and mlist, but it can turn out a bit cumbersome.

The pairs in #lang r5rs or #lang sicp are mutable which means, you can use the SICP code as is.

https://docs.racket-lang.org/sicp-manual/SICP_Language.html?q=sicp

In case you wonder why pairs are immutable in #lang racket, see: https://blog.racket-lang.org/2007/11/getting-rid-of-set-car-and-set-cdr.html

Upvotes: 6

Related Questions