(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:
Anonymous2008-10-04 1:14
public class Pair extends Object{
public Object car;
public Object 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:
Anonymous2008-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 )
>>7
Hey, we should compare our printf and malloc implementations. I bet mine would perform better when I apply some funroll loops
Name:
Anonymous2008-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