Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Puzzling CONS definition from SICP

Name: Anonymous 2010-12-29 6:27

I tried to use it, but couldnt find a way to discern it from ordinary function.

(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
          ((= m 1) y)
          (else (error "Argument not 0 or 1 -- CONS" m))))
  dispatch)

(define (car z) (z 0))

(define (cdr z) (z 1))

Name: Anonymous 2010-12-29 8:15

>>1
It's not different from an ordinary function in your example. It's just an example that shows that a cons could be implemented as a closure. What exactly is a cons? Something that holds a pair of two objects somehow. The internals are irrelevant for that example. What's relevant is that you can access a cons through an interface (car/cdr at least), which is what you do up there. In a real implementation, cons is a real data structure, and you would be able to define all kinds of data structures and so on, for efficiency reasons, but if not for that, you could just use closures for most data structures and it would work just fine.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List