DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Procedures As Data Structures

  • submit to reddit
        The notion of a 'pair' in lisp. A 'pair' is a way of glueing two pieces of data.
(define z (cons x y))
(car z) => x
(cdr z) => y
The interesting thing is that any triple of procedures that satisfies the above condition can be used as the basis for implementing pairs. This is one such triple:

(define (cons x y)
(lambda (m) (m x y)))

(define (car z)
(z (lambda (p q) p)))

(define (cdr z)
(z (lambda (p q) q)))
Implementation of cons, car, and cdr without using any data structures at all but only using procedures. This blurs the distinction between 'procedure' and 'data'.