21

I am just trying to do very simple code with set-car! and set-cdr! in racket, but I got the error: expand: unbound identifier in module in: set-car! and expand: unbound identifier in module in: set-cdr!

Aren't they defined in racket? Could anyone help?

SoftTimur
  • 5,630
  • 38
  • 140
  • 292
  • Which language do you choose? Set the language to R5RS. – pjhades Feb 28 '12 at 02:07
  • I have `#lang racket` in the beginning, when I set `#lang R5RS`, it gives me an error: `Module Language: invalid module text standard-module-name-resolver: collection not found: # in any of: (# #)` – SoftTimur Feb 28 '12 at 02:13
  • 7
    Use `#lang r5rs` -- lower case. (But using the r5rs language is not going to make for a pleasant experience.) – Eli Barzilay Feb 28 '12 at 08:23

3 Answers3

32

You need to import mutable-pairs-6, like this:

(require rnrs/mutable-pairs-6)

Those procedures were moved to a different module and renamed to mcons, mcar, mcdr, set-mcar!, set-mcdr!, mlist to emphasize that they operate on mutable data, unlike their immutable counterparts.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
12

Óscar López's answer is correct, but doesn't explain why normal pairs are immutable.

In Racket (and its predecessor, PLT Scheme, since version 4), cons cells are immutable by default. Most Scheme programs never need to modify cons cells, and having them be immutable allows many optimisations. (For example, list? and length can both be constant-time.)

For cases where mutability is needed, there's mpair (as mentioned), and more usefully, there's boxes.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
  • 9
    [Obligatory reference here.](http://blog.racket-lang.org/2007/11/getting-rid-of-set-car-and-set-cdr.html) – Eli Barzilay Feb 28 '12 at 02:47
  • SICP Chapters 4 and 5 use mutable cons cells for the metacircular evaluator and for register machines, so the code from the book (https://mitpress.mit.edu/sites/default/files/sicp/code/index.html) doesn't quite run 'out-of-the-box' on Racket v7.2. – Reb.Cabin Mar 17 '19 at 11:56
  • You can use the package neil/sicp with racket to get set-car! and set-cdr! for the SICP. I'm doing it right now, and it looks like it works. Something like that in the command line works: ` racket -i -p dyoo/simply-scheme -p neil/sicp -l xrepl ` - see https://planet.racket-lang.org/package-source/neil/sicp.plt/1/18/planet-docs/sicp/index.html – Matthieu Sep 18 '20 at 17:34
1

Assuming that the issue is about section 3.4 (Concurrency) in SICP, here's the code to implement parallel-execute and make-serializer:

(require rnrs/mutable-pairs-6)
(require compatibility/mlist)

(define (parallel-execute . procs)
  (map thread-wait
       (map (lambda (p) (thread p))
            procs)))

(define (make-serializer)
  (let ((mutex (make-mutex)))
    (lambda (p)
      (define (serialized-p . args)
        (mutex 'acquire)
        (let ((val (apply p args)))
          (mutex 'release)
          val))
      serialized-p)))

(define (make-mutex)
  (let ((cell (mlist false)))
    (define (the-mutex m)
      (cond ((eq? m' acquire)
             (if (test-and-set! cell)
                 (the-mutex 'acquire)
                 (void)))
            ((eq? m 'release)
             (clear! cell))))
    the-mutex))

(define (clear! cell)
  (set-mcar! cell false))

(define (test-and-set! cell)
  (if (mcar cell)
      true
      (begin (set-mcar! cell true)
             false)))
Patrick Bucher
  • 1,302
  • 2
  • 14
  • 36