## 0 Replies - 1494 Views - Last Post: 22 June 2011 - 02:09 PM

### #1 I X Code X 1 Reputation: 47
• Posts: 66
• Joined: 28-July 10

# [Lisp] Make Points

Posted 22 June 2011 - 02:09 PM

Description: Should work for any Common Lisp implementation; I used Allegro Common Lisp.This snippet, demonstrates how one could go about making points in common lisp. When I say make points, I mean like Java's point class. It can be given any number of points and pairs them together.
```;; In Java we might use something like this to make a point
;; Point p = new Point(1,2);

;; In common lisp we can make dotted pairs like this:
;; (cons 1 2) -> (1 . 2)

;; However, that is not going to work well if we want to make
;; many points at once. Instead, we can use this function:

(defparameter *y* '())

(defun make-point (list)
(let ((x (cons (first list) (second list))))
(setf *y* (push x *y*))
(if (cddr list)
(make-point (cddr list))
(progn (print (reverse *y*))
(setf *y* '())))))

CG-USER(57): (MAKE-POINT '(1 2 3 4 5 6 7 8 9 10))

((1 . 2) (3 . 4) (5 . 6) (7 . 8) (9 . 10))

;; This simply accumulates the pairs until the list is empty.
;; However, it's not very "lispy"

;; The way you want to think about this is that as the call stack unwinds,
;; the result is accumulated:

(defun make-point-r (list)
(cond
((null list) nil)
(t (cons (cons (car list) (cadr list)) (make-point-r (cddr list))))))

;; This works the same way, but no external storage and no reverse!

* (make-point-r  '(1 2 3 4 5 6 7 8 9 10))

((1 . 2) (3 . 4) (5 . 6) (7 . 8) (9 . 10))

```

Is This A Good Question/Topic? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }