;;; Exercice 1 (defun entier (chiffres &optional (base 2)) (let ((entier 0)) (dolist (chiffre chiffres entier) (setf entier (+ (* entier base) chiffre))))) ;;; Exercice 2 (defun serie (u0 f p) (do* ((i 0 (1+ i)) (u u0 (funcall f u)) (som u (+ som u))) ((= i p) som))) ;; ou avec loop (defun serie (u0 f p) (loop for i from 0 to p and u = u0 then (funcall f u) sum u)) ;;; Exercice 3 (defun next-line (line) (let* ((length (1+ (length line))) (next-line (make-array length :initial-element 1))) (loop for i from 1 below (1- length) do (setf (aref next-line i) (+ (aref line (1- i)) (aref line i)))) next-line)) ;;; Exercice 4 ;; 1 (defun mot-suivant (mot table) (let* ((reps (gethash mot table)) (n (length reps))) (cond ((zerop n) nil) ((= n 1) (first reps)) (t (nth (random (1- n)) reps))))) ;; 2 (defun simul-dialogue (mot table) (do ((mot mot (mot-suivant mot table))) ((null mot)) (print mot))) ;; ou avec loop (defun simul-dialogue (mot table) (loop for mot-courant = mot then (mot-suivant mot-courant table) while mot-courant do (print mot-courant)))