A very nice section of SICP[1] describes how cons/car/cdr can be built only
with functions. Translated to Clojure it might look like
(defn cons [a b]
#(condp = %
:car a
:cdr b))
(defn car [cons-cell]
(cons-cell :car))
(defn cdr [cons-cell]
(cons-cell :cdr))
(car (cons :a :b)) => :a
(cdr (cons 1 2)) => 2
An excellent example of closures and the use of higher order functions.
Pairs can also serve as nice example usage for defprotocol and reify:
(defprotocol Pair
(car [cons-cell])
(cdr [cons-cell]))
(defn cons [a b]
(reify Pair
(car [_] a)
(cdr [_] b)))
(car (cons :a :b)) => :a
(cdr (cons 1 2)) => 2
[1]: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-14.html#%_sec_2.1.3
On Saturday, June 16, 2012 6:35:14 PM UTC+3, octopusgrabbus wrote:
>
> I have a need to learn enough scheme to read it and write a few functions.
> I came across dotted pair notation. I am trying to grok it in terms of the
> only Lisp I know, Clojure. Does dotted pair notation in Scheme compare to
> form in Clojure, and if so, how?
>
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en