;;;;;;;;;; Exercice 4.1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defconstant empty-set '()) (defparameter s1 empty-set) (defun set-emptyp (s) (endp s)) ; (set-emptyp s1) (defun set-adjoin (x s &key (test #'equal)) (adjoin x s :test test)) ; (set-adjoin 1 s1 :test #'=) ; s1 ; (setf s1 (set-adjoin 1 s1 :test #'=)) ; s1 ; (setf s1 (set-adjoin 2 s1 :test #'=)) ; (setf s1 (set-adjoin 2 s1 :test #'=)) (defun set-from-list (l &key (test #'eql)) (remove-duplicates l :test test)) ;; (set-from-list '(a b c a b a)) ;; (set-from-list '((1 1 3) (1 2) (2 1) (1 2) (2 3))) ;; (set-from-list '((1 1 3) (1 2) (2 1) (1 2) (2 3)) :test #'equal) ;;; version naïve récursive (defun set-from-list-rec (l &key (test #'eql)) (if (endp l) the-empty-set (let ((s (set-from-list (cdr l) :test test))) (if (set-member (car l) s :test test) s (cons (car l) s))))) ;;; version naïve récursive (defun set-from-list-it (l &key (test #'eql)) (do ((l l (cdr l)) (acc nil (adjoin (first l) acc :test test))) ((endp l) acc))) (defun set-member (x s &key (test #'eql) ) (car (member x s :test test))) ;;;;;;;;;; Exercice 4. 2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defparameter *m* 200) (defparameter *n* 11) (defun make-histogram (n) (make-array n)) (defparameter *histogram* (make-histogram *n*)) (defun record-value (k histo) (setf (aref histo k) (1+ (aref histo k)))) (dotimes (i *m*) (record-value (random *n*) *histogram*)) (defun print-histogram (histo) (dotimes (i (length histo)) (format t "~2D " i) (print-hist-line (aref histo i)))) (defun print-hist-line (k) (format t "[~3D] " k) (dotimes (i k) (format t "*")) (format t "~%"))) (print-histogram *histogram*)