;;; Exercice 1 (cons 1 2) ;; => (1 . 2) (cons 1 (cons 2 nil)) ;; => (1 2) (list 'a (+ 1 2) ()) ;; => (A 3 NIL) (append '(a) (list 1 2) ()) ;; => (A 1 2) ;;; Exercice 2 ;; 1. (mystere1 '(1 4 3 2 5 6) #'evenp) ;; => ((4 2 6) (1 3 5)) ;; 2. si "pred" est purement fonctionnel alors "mystere1" et "mystere2" ;; sont équivalentes ;; 3. (defun mystere2 (l pred) (values (remove-if-not pred l) (remove-if pred l))) ;; 4. (mystere2 '(1 4 3 2 5 6) #'evenp) ;; (4 2 6) ;; (1 3 5) ;; 5. (defun mystere1 (l pred) (labels ((aux (l ok not-ok) (cond ((endp l) (list (nreverse ok) (nreverse not-ok))) ((funcall pred (car l)) (aux (cdr l) (cons (car l) ok) not-ok)) (t (aux (cdr l) ok (cons (car l) not-ok)))))) (aux l () ()))) ;;; Exercice 3 ;; 1. (macroexpand-1 '(with-staff-size 12 (write-line) (write-line))) ;; => ;; (LET ((SIZE-VAR 12)) ;; (UNLESS (AREF *FONTS* SIZE-VAR) ;; (SETF (AREF *FONTS* SIZE-VAR) (MAKE-FONT SIZE-VAR))) ;; (LET ((*FONT* (AREF *FONTS* SIZE-VAR))) ;; (WRITE-LINE) ;; (WRITE-LINE))) ;; T ;; 2. capture possible d'une variable "size-var" ;; 3. solution: utiliser un symbole fourni par "gensym" (defmacro with-staff-size (size &body body) (let ((size-var (gensym))) `(let ((,size-var ,size)) (unless (aref *fonts* ,size-var) (setf (aref *fonts* ,size-var) (make-font ,size-var))) (let ((*font* (aref *fonts* ,size-var))) ,@body)))) ;;; Exercice 4 ;; 1. "conic" est dérivée de "region". "circle" et "ellipse" sont dérivées de "conic" ;; 2. (defun make-ellipse (axe-x axe-y &optional (center #C(0 0))) (make-instance 'ellipse :center center :axe-x axe-x :axe-y axe-y)) ;;3. (defmethod area ((ellipse ellipse)) (* pi (axe-x ellipse) (axe-y ellipse))) ;; 4. (defmethod bounding-box ((ellipse ellipse)) (let* ((axe-x (axe-x ellipse)) (axe-y (axe-y ellipse)) (center (center ellipse)) (x (realpart center)) (y (imagpart center))) (values (complex (- x axe-x) (- y axe-y)) (complex (+ x axe-x) (+ y axe-y))))) ;; 5. (defmethod print-object ((ellipse ellipse) stream) (call-next-method) (format stream " Axe-x:~A Axe-y:~A)" (axe-x ellipse) (axe-y ellipse)))