1

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.

2 Answers 2

0

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.

6
  • It's a good alternative. But I want to know whatneil/scip have done, and why it doesn't define when since it's a support for sicp.
    – Windor C
    Apr 10, 2014 at 3:09
  • @WindorC when is not part of the standard Scheme language, the implementors of the package simply chose not to support it. It's not essential, it's just syntactic sugar for (if <condition> (begin <expressions>) (void)). In fact, as far as I remember when is never mentioned in SICP, so what would be the point of adding it to the package? Apr 10, 2014 at 3:13
  • @WindorC just to be clear: Racket and Scheme are different languages; SICP, and by extension the neil/sicp package, deal with Scheme - so you can't expect the package to support all syntactic forms from Racket Apr 10, 2014 at 3:23
  • Thanks for explanation, I know the two are different, but I do not know Racket has gone so far. Maybe it's not a good choice for learning sicp, or I miss something when using Racket? By the way, void also comes to a unbound identifier in module.
    – Windor C
    Apr 10, 2014 at 3:40
  • @WindorC Racket is a fine choice for going through SICP, as long as you use one of the support packages and stick to the book, avoiding forms which are not used in the book. If (void) doesn't work, replace it with #f, it's just a way to say: "ignore the return value". And feel free to accept this answer if it was helpful for you ;) Apr 10, 2014 at 14:49
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.