Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

What is Scheme's dotted pair construct analogous to in other Lisp implementations? I can make a vector or list quite easily, and understand those in Clojure, even though the syntax is a little different, like Clojure's vectors use square brackets [].

However, seeing a dotted pair for the first time threw me. It almost looks like it is an implementation of of map.

I'm not looking for a discussion, but more for use or the dotted pair equivalent in other Lisp dialects, like Clojure, or even Python.

share|improve this question

2 Answers 2

up vote 4 down vote accepted

Common Lisp (implementation: SBCL)

* (cons 1 2)

(1 . 2)
* (car '(1 . 2))

1
* (cdr '(1 . 2))

2

I'm pretty sure that's standard Common Lisp.

Here's some Clojure:

Clojure 1.3.0
user=> (cons 1 2)
IllegalArgumentException Don't know how to create ISeq from: java.lang.Long  clojure.lang.RT.seqFrom (RT.java:487)
user=> (car '(1 . 2))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: car in this context, compiling:(NO_SOURCE_PATH:2) 
user=> (cdr '(1 . 2))
CompilerException java.lang.RuntimeException: Unable to resolve symbol: cdr in this context, compiling:(NO_SOURCE_PATH:3) 
user=> '(1 . 2)
(1 . 2)
user=> (first '(1 . 2))
1
user=> (rest '(1 . 2))
(. 2)

I'm really not a Clojure expert (or a Common Lisp expert), but I'm not sure Clojure has anything that supports an improper list (like '(a b . c)) built in.

share|improve this answer
    
Are you saying that cons is a list pair builder? You're right there's nothing like it I know of in Clojure. –  octopusgrabbus Jun 16 '12 at 10:26
    
In other LISPs (cons) is a list pair builder. It looks from the first error message that (cons) is a Clojure function but doesn't allow arbitrary data as the second argument. –  yakiv Jun 16 '12 at 17:46

There's no dotted pair in Clojure.

A philosophical reason for this is that Clojure avoids the use of a concrete "pair" data structure and instead emphasises abstract "sequences" which can have may possible concrete implementations.

The closest equivalents are list and cons:

;; construct a sequence by consing a value onto any sequential object (here a vector)
(cons 1 [2 3 4])
=> (1 2 3 4)

;; construct a list
(list 1 2 3 4)
=> (1 2 3 4)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.