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

Pages: 1-

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-03 23:49

car :: [a] -> a
car = head
cdr :: [a] -> [a]
cdr = tail

Name: Anonymous 2008-10-04 0:27

function cons(h, t)
  { car: h, cdr: t }
function car(p) p.car
function cdr(p) p.cdr


JavaScript 1.9. Or...

function cons(fst, snd) {
  return function(sel) {
    return sel == 0 ? fst : snd
  }
}

function car(p) { return p(0) }
function cdr(p) { return p(1) }

Name: Anonymous 2008-10-04 0:42

>>2
Thread over.

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)

Name: Anonymous 2008-10-04 1:14

public class Pair extends Object{
  public Object car;
  public Object cdr;
}

public static Pair cons(Object car, Object cdr) {
  Pair toCons = new Pair();
  toCons.car = car;
  toCons.cdr = cdr;
  return toCons;
}

Name: Anonymous 2008-10-04 5:30

>>2
only koons use type signatures and built in functions

Name: Anonymous 2008-10-04 6:29

>>5
I like this one more.
(define (cons x y)
(lambda (f) (f x y)))

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

(define (cdr x)
  (x (lambda (x y) y)))

Name: Anonymous 2008-10-04 6:39

cons([CAR|CDR],CAR,CDR). % 3 birds with one HORN CLAWS

Name: Anonymous 2008-10-04 6:46

Simple crap:

cons = lambda x, y: (x, y)
car = lambda x: x[0]
cdr = lambda x: x[1]


Functional fapping:

cons = lambda x, y: lambda f: f(x, y)
car = lambda x: x(lambda x, y: x)
cdr = lambda x: x(lambda x, y: y)


OOP fapping:

class cons(object):
    def __init__(self, car, cdr):
        self.car, self.cdr = car, cdr
car = lambda c: c.car
cdr = lambda c: c.cdr


The last implementation is the best, because it introduces semantics (you can check whether something is a cons cell with type(x) is cons; in other words you can implement pair?, which can be done in the second version but you have to check the func_code.co_code), and because you can use it with either the car and cdr functions, or the car and cdr properties.

Name: Anonymous 2008-10-04 6:57

    3. car. car[x] is defined if and only if x is not atomic. car [(e1 · e2 )] = e1 .
Thus car [X] is undefined.
car [(X · A)] = X
car [((X · A) · Y )] = (X · A)
    4. cdr. cdr [x] is also defined when x is not atomic.           We have cdr
[(e1 · e2 )] = e2 . Thus cdr [X] is undefined.
cdr [(X · A)] = A cdr [((X · A) · Y )] = Y
    5. cons. cons [x; y] is defined for any x and y. We have cons [e1 ; e2 ] =
(e1 · e2 ). Thus
cons [X; A] = (X A)
cons [(X · A); Y ] = ((X · A)Y )

Name: Anonymous 2008-10-04 10:07

>>11
WTF is this shit

Name: Anonymous 2008-10-04 10:12

>>7
Hey, we should compare our printf and malloc implementations. I bet mine would perform better when I apply some funroll loops

Name: Anonymous 2008-10-04 10:42

>>12
Recursive Functions of Symbolic Expressions
  and Their Computation by Machine, Part I
John McCarthy, Massachusetts Institute of Technology, Cambridge, Mass.
                       April 1960


Son, you just defiled a cudder.

Name: Anonymous 2008-10-05 13:41

>>10
IDIOT

Name: Anonymous 2008-10-05 14:33

>>15
Why?

Name: Anonymous 2008-10-05 15:38

: car @ ;
: cdr cell+ @ ;
: cons 2 cells allocate throw tuck cell+ ! tuck ! ;

Name: Anonymous 2008-10-05 15:49

>>17
lol ?

Name: Anonymous 2008-10-05 17:01

>>18
What?

Name: Anonymous 2008-10-05 21:51

Q: What's The Sussman's favorite nursery rhyme?
A: Mary Had a Little Lambda

Q: What did the rude Norwegian call Sussman and Abelson?
A: Jävla ködder

Q: What does The Sussman taste like?
A: Salty milk and cons

Name: Anonymous 2008-10-06 8:15

>>20
That's Swedish.

Jævla kødder

Name: Anonymous 2008-10-06 11:56

>>21
Faen

Name: Anonymous 2008-10-06 15:30

>>22
Cudder

Name: Anonymous 2008-10-06 21:07

>>17
More accurate version:

: car dup if @ endif ;
: cdr dup if cell+ @ endif ;
: cons 2 cells allocate throw tuck cell+ ! tuck ! ;

Name: Trollbot9000 2009-07-01 8:27

Fapping class cons object car Object cdr.

Name: Anonymous 2010-12-17 1:32

Are you GAY?
Are you a NIGGER?
Are you a GAY NIGGER?

If you answered "Yes" to all of the above questions, then GNAA (GAY NIGGER ASSOCIATION OF AMERICA) might be exactly what you've been looking for!

Name: Anonymous 2011-02-04 14:46

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