Question

I was solving exercise-3.23 of section 3.3.2 of SICP, which is an implementation of deque.

As Racket doesn't support set-car! and set-cdr!,so I use #lang planet neil/sicp from SICP Support for DrRacket,and wrote the code:

#lang planet neil/sicp

(define (make-deque) (cons '() '()))
(define (empty-deque? dq) (null? (front-ptr dq)))
;;; just here, I use when form
(define (front-delete-deque! dq)
  (cond ((empty-deque? dq)
         (error "FRONT-DELETE-DEQUE! called with empty deque" dq))
        (else
         (set-car! dq (caddr (front-ptr dq)))
         (when (null? (front-ptr dq))
           (set-cdr! dq '())))))
(define (front-ptr dq) (car dq))
(define (rear-ptr dq) (cdr dq))

I got an error: when: unbound identifier in module, which is very strange.

I think it has something to do with the neil/sicp, since Racket has when form.

And can someone explain what exactly #lang planet neil/sicp means and matters?

PS: forget how I implement the deque.

Was it helpful?

Solution

It's quite possible that the implementors of the neil/sicp package chose not to support when - after all, it's not part of the standard language and (as far as I can remember) it was never mentioned in SICP. But fear not, when is not essential and you can write something equivalent, just substitute this part:

(when (null? (front-ptr dq))
  (set-cdr! dq '()))

With this:

(if (null? (front-ptr dq))
  (set-cdr! dq '())
  #f)

And if you're feeling bold you can even write your own when macro, one of the nice things of Scheme is that you can extend its syntax. Left as an exercise for the reader.

OTHER TIPS

SICP is beautiful stuff; beautifully designed to work with Scheme, plain and simple. Rather than dealing with the idiosyncrasies of Racket, such as not having set-car! apparently, why not just choose a proper Scheme?

There are different Scheme standards. The most recent, R7RS, defines when and provides a definition as:

(define-syntax when
  (syntax-rules ()
    ((when test result1 result2 ...)
     (if test
         (begin result1 result2 ...)))))

If you use a Scheme based on R5RS or R6RS they will either already define when or allow you to define it as above.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top