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

ITT: cons in our favorite language

Name: Anonymous 2008-10-03 23:43


typedef struct
{
    void * car;
    void * cdr;
}pair;

pair * cons(void * car, void * cdr)
{
    pair * toCons = malloc(sizeof(pair));
    toCons->car = car;
    toCons->cdr = cdr;
    return toCons;
}

Name: Anonymous 2008-10-04 1:00

(excerpted from The Structure and Interpretation of Computer Programs by Hal Abelson and Gerald Jay Sussman, sec. 2.1.3.)
(define (cons x y)
  (define (dispatch m)
    (cond ((= m 0) x)
          ((= m 1) y)
          (else (error "Argument not 0 or 1 -- CONS" m))))
  dispatch)

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