Created
June 12, 2011 19:41
-
-
Save pelletier/1021920 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; Idp | |
;;; Identity predicate. Test if all members of the vector are equals. | |
;;; | |
;;; CL-USER> (idp (list 2 3 4 5)) | |
;;; NIL | |
;;; CL-USER> (idp (list 2 2 2 2)) | |
;;; T | |
(defun idp (v) | |
(if (null v) | |
t | |
(let ((r (reduce #'(lambda (x y) (if (eql x y) x nil)) v))) | |
(if (not (null r)) t)))) | |
;;; Average | |
;;; Compute the average value of a N^n vector v. | |
(defun average (v) | |
(if (null v) | |
(error "The average value of an empty list isn't defined.") | |
(/ (reduce #'+ v) | |
(length v)))) | |
;;; Shrink | |
;;; Reduce a N^n vector v to the average av. | |
;;; | |
(defun shrink (v av) | |
(if (null v) | |
nil | |
(cons (let ((a (car v))) | |
(if (= (- a av) 0) | |
a | |
(- a (/ (- a av) (abs (- a av)))))) | |
(shrink (cdr v) av)))) | |
;;; Print-to-file | |
;;; Print the list of vectors to a file. | |
(defun print-to-file (l) | |
(with-open-file (strm "./output.txt" :direction :output :if-exists :supersede) | |
(loop for vect in l do | |
(loop for nbr in vect do | |
(format strm (write-to-string nbr))) | |
(format strm "~%")) )) | |
;;; COD | |
;;; Loop to reduce the vector. | |
(defun cod (v) | |
(let ((av (round (average v))) (acc (cons v nil))) | |
(loop | |
(print (car acc)) | |
(when (idp (car acc)) (return acc)) | |
(setf acc (cons (shrink (car acc) av) acc))) | |
(print-to-file (reverse acc)))) | |
(cod (list 0 4 2 3 54 12 21 44 32 50)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment