;;; Exercice 1 ; 1.1 ((A B) C D) ; 1.2 (C D) ; 1.3 (A B (E G) (H I)) ; 1.4 (20 40 60) ; 1.5 ((1) (2) (3)) ; 1.6 The value (3 3 4) is not of type NUMBER. ; [Condition of type TYPE-ERROR] ; (apply #'+ (list (1+ 2) 3 4)) aurait donné 10 ;;; Exercice 2 ; 2.1 ((A B C) (D E) (A B)) ; 2.2 (mystere-it l) construit une liste contenant les mêmes éléments que l ; mais sans doublon (au sens de equalp) ; 2.3 (defun mystere-rec (l) (if (endp l) '() (let ((s (mystere-rec (cdr l)))) (if (member (car l) s :test #'equal) s (cons (car l) s))))) ; 2.4 (remove-duplicates '((a b) (d e) (a b c) (d e) (a b)) :test #'equalp) ;;; Exercice 3 ; 3.1 (defun trinome (a b c) (lambda (x) (+ (* a x x) (* b x) c))) ; 3.2 (funcall (trinome 1 2 3) 2) ;;; Exercice 4 ;;; 4.1 (defstruct point2d (id 0) (x 0.0) (y 0.0)) (defstruct point3d (id 0) (x 0.0) (y 0.0) (z 0.0) (points2d (make-hash-table))) ;; 4.1-bis. On peut aussi spécifier les types, mais ce n'était pas demandé. (defstruct point2d (id 0 :type integer) (x 0.0 :type real) (y 0.0 :type real)) (defstruct point3d (id 0 :type integer) (x 0.0 :type real) (y 0.0 :type real) (z 0.1 :type real) (points2d (make-hash-table) :type hash-table)) ;;; 4.2 (defstruct (point2d (:print-function (lambda (p stream depth) (declare (ignore depth)) (format stream "Point2d id=~A (~A,~A)" (point2d-id p) (point2d-x p) (point2d-y p))))) (id 0) (x 0.0) (y 0.0)) ;; Variante avec fonction définie séparément: (defstruct (point2d (:print-function my-print-point2d)) (id 0) (x 0.0) (y 0.0)) (defun my-print-point2d (p stream depth) (declare (ignore depth)) (format stream "Point2d id=~A (~A,~A)" (point2d-id p) (point2d-x p) (point2d-y p))) ;;; 4.3 (defvar *test-point2d*) (setq *test-point2d* (make-point2d :id 3 :x 3/4 :y 31/5)) ;;; 4.4 (defun make-point2d-middle (id point2d-1 point2d-2) (make-point2d :id id :x (/ (+ (point2d-x point2d-1) (point2d-x point2d-2)) 2) :y (/ (+ (point2d-y point2d-1) (point2d-y point2d-2)) 2))) ;;; 4.5 (defun put-point2d (point3d point2d) (let ((table (point3d-point2d point3d)) (id (point2d-id point2d))) (setf (gethash id table) point2d))) ;;; 4.6 ;; version qui marche, mais qui parcourt *toutes* les entrées de ;; la table de hachage ;; de plus, allocation [lente] d'une liste temporaire (defun suppress-points2d (point3d min-id max-id) (assert (>= max-id min-id)) (let ((acc nil) (table (point3d-points2d point3d))) (maphash (lambda (key val) (when (and (>= key min-id) (<= key max-id)) (push key acc))) table) (mapcar (lambda (id) (remhash id table)) acc))) ;; version identique, mais `remhash' dans le `maphash'. ;; cf la doc. de `maphash' : c'est autorisé. ;; meilleure utilisation de '<='. (defun suppress-points2d (point3d min-id max-id) (assert (>= max-id min-id)) (let ((table (point3d-points2d point3d))) (maphash (lambda (key val) (when (<= min-id key max-id) (remhash key table))) table))) ;; version qui va faire le minimum d'itérations (defun suppress-points2d (point3d min-id max-id) (assert (>= max-id min-id)) (let ((table (point3d-points2d point3d))) (do ((id min-id (1+ id))) ((> id max-id)) (remhash id table))))