Skip to main content

Scheme: Implementing cons, car, and cdr

I'm going through Structure and Interpretation of Computer Programs per the recommendation of my buddy Mike Cheponis. I'm really enjoying it.

I always thought cons, car, and cdr were so low-level that they had to be builtins. However, interestingly enough, SICP shows how they could be implemented using a closure:
(define (my-cons a b)
(lambda (pick)
(cond ((= pick 1) a)
((= pick 2) b))))

(define (my-car x) (x 1))
(define (my-cdr x) (x 2))
It's kind of silly, but it also works in Python:
def cons(a, b):

def list(pick):
if pick == 1:
return a
elif pick == 2:
return b
raise ValueError

return list

def car(list):
return list(1)

def cdr(list):
return list(2)

It's easy to see how to extend this in Scheme to have "objects" with any number of memebers, although I'm sure it's not very efficient.

By the way, I really like DrScheme. It's relatively modern and very friendly.


Bill Mill said…
I'm doing SICP with DrScheme as well, and finding the environment quite pleasant. For example, it was easy to figure out how to plot the runtimes in the runtime complexity section.

Scheme, however, I'm still not all that impressed with. Maybe when I get to the macros that'll improve.
Titus Brown said…
Clobbering the 'list' type constructor, eh?
jjinux said…
> Clobbering the 'list' type constructor, eh?

I wouldn't usually do it in practice, but neither would I implement cons, car, and cdr in practice. Naming it "list" makes this relatively clever code more understandable. It's a function that's treated as a list. Clever. Guess I could have named it list_.
jjinux said…
> Scheme, however, I'm still not all that impressed with. Maybe when I get to the macros that'll improve.

What amazes me is that Lisp is such a powerful language from so long ago. Lisp is 40 years old!
Rob Hunter said…
Thanks for the post. It reminded me that there's an even wackier impl of cons, car and cdr in SICP a little ways down from there:

(define (cons x y)
  (lambda (picker-f)
    (picker-f x y)))

(define (car pair)
  (pair (lambda (x y) x)))

(define (cdr pair)
  (pair (lambda (x y) y)))
jjinux said…
That's even nicer. You don't need an if statement or a cond.
jjinux said…
> It reminded me that there's an even wackier impl of cons, car and cdr in SICP a little ways down from there:

I now know that that trick was created by Alonso Church in the 1930's, way before there were computers to try it on ;)
jjinux said…
Actually, I think it's spelled Alonzo Church.
Anonymous said…
Hah. Found this through google search -- I remember being shown this trick as a freshman in CS about 12 years ago, and as I was going to sleep last night, it came to mind.

(I've been learning Haskell lately, that's probably why.)

Aha, here we go:
jjinux said…

It's a cute trick. For some reason, I have Haskell stuck on my mind lately. I have a decent beginner's grasp of it, but I keep thinking I should go further. Have you read that new book "Real World Haskell", and if so, do you have any comments on it?


Popular posts from this blog

Ubuntu 20.04 on a 2015 15" MacBook Pro

I decided to give Ubuntu 20.04 a try on my 2015 15" MacBook Pro. I didn't actually install it; I just live booted from a USB thumb drive which was enough to try out everything I wanted. In summary, it's not perfect, and issues with my camera would prevent me from switching, but given the right hardware, I think it's a really viable option. The first thing I wanted to try was what would happen if I plugged in a non-HiDPI screen given that my laptop has a HiDPI screen. Without sub-pixel scaling, whatever scale rate I picked for one screen would apply to the other. However, once I turned on sub-pixel scaling, I was able to pick different scale rates for the internal and external displays. That looked ok. I tried plugging in and unplugging multiple times, and it didn't crash. I doubt it'd work with my Thunderbolt display at work, but it worked fine for my HDMI displays at home. I even plugged it into my TV, and it stuck to the 100% scaling I picked for the othe

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;) In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3 , Erlang , and the original Lisp machine . The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts. Now, I'm sure that many of these ideas have already been envisioned within , LLVM , Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway. Rather than wax philosophical, let me just dump out some ideas: Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code.&quo

Haskell or Erlang?

I've coded in both Erlang and Haskell. Erlang is practical, efficient, and useful. It's got a wonderful niche in the distributed world, and it has some real success stories such as CouchDB and Haskell is elegant and beautiful. It's been successful in various programming language competitions. I have some experience in both, but I'm thinking it's time to really commit to learning one of them on a professional level. They both have good books out now, and it's probably time I read one of those books cover to cover. My question is which? Back in 2000, Perl had established a real niche for systems administration, CGI, and text processing. The syntax wasn't exactly beautiful (unless you're into that sort of thing), but it was popular and mature. Python hadn't really become popular, nor did it really have a strong niche (at least as far as I could see). I went with Python because of its elegance, but since then, I've coded both p