Just a quick note about SICP, mutable pairs, and PLTScheme

I was going through exercises in Section 3.3 of SICP recently (Modeling with Mutable Data), and discovered that my version of PLTScheme (4.1.4) does not include set-car! and set-cdr! operators. It turns out the team that maintains this development environment (now called “Racket”) changed this in Version 4. Originally Scheme had mutable pairs by default. So when you used cons, it created a mutable pair, though it treated it as immutable if you used car and cdr on it. You had to use set-car! and set-cdr! to change a pair’s contents. The dev. team changed PLTScheme such that cons creates immutable pairs (and car and cdr operate on it in an immutable fashion as usual). To use mutable pairs you need to use mcons, mcar, and mcdr to do the same operations that you used cons, car, and cdr to carry out on immutable pairs. Where the SICP text says to use set-car!, and set-cdr!, to manipulate mutable pairs, you need to use set-mcar!, and set-mcdr!.

This change only applies to the issue of immutable vs. mutable pairs. The dev. team made this decision, because in their view it made Scheme more of a pure functional language. However, I noticed that the set! operator (which changes a binding) still exists and works as expected in my copy of PLTScheme.

Edit: I goofed a bit when I posted this earlier today. I said that PLTScheme users should use mcar and mcdr to carry out the same operations as set-car! and set-cdr! in the SICP text. That is not the case. People should use set-mcar! and set-mcdr! for those operations.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s