Search code examples
if-statementschemeracketsicp

Why cannot find `when` form when I use neil/sicp?


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.


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.