next up previous
Contents Next: defun (MACRO) Up: Appendix: Selected Lisp Previous: cond (MACRO)

cons (FUNCTION)

Format: (cons <exp1> <exp2> )

Required arguments: 2

<exp1> : any Lisp expression <exp2> : any Lisp expression

Both argument expressions are evaluated. In common usage, the second expression usually returns a list. In this case, cons creates a new copy of the list returned by <expr2> , and makes the value returned by <expr1> the new first element in this list. However, if <expr2> returns an atom, cons returns the dotted pair of the values of the two expressions.

Examples:

>  (cons 'a '(1 2 3))
(A 1 2 3)

>  (cons '(a) '(1 2 3))
((A) 1 2 3)

>  (setq a 3)
3
>  (cons a (list 'i 'j 'k))
(3 I J K)

>  (cons 'a a)
(A . 3)                           ;;a dotted pair

>  (cons '(1 2) 'q)
((1 2) . Q)                       ;;another dotted pair



© Colin Allen & Maneesh Dhagat
March 2007