;;; Exercice 1 (defun premier-p (n) (assert (integerp n)) (when (or (<= n 1) (and (> n 2) (evenp n))) (return-from premier-p nil)) (loop for d from 3 to (sqrt n) by 2 when (zerop (mod n d)) do (return-from premier-p nil) finally (return t))) ;;; Exercice 2 (defun changements (tab) (let ((len (length tab))) (if (zerop len) 0 (loop with changements = 1 for i from 1 below len with v = (aref tab 0) unless (= v (aref tab i)) do (progn (incf changements) (setf v (aref tab i))) finally (return changements))))) ;;; Exercice 3 (defun max-tab (tab) (loop for e across tab maximize e)) (defun max-tab (tab) (reduce #'max tab :initial-value 0)) (defun frequence (tab) (let ((len (length tab)) (fr (make-array (1+ (max-tab tab))))) (dotimes (i len fr) (incf (aref fr (aref tab i)))))) ;;; Exercice 3 (defparameter *t1* (let ((table (make-hash-table :test #'eq))) (setf (gethash 'toi table) nil) (setf (gethash 'moi table) nil) (setf (gethash 'lui table) nil) table)) (defparameter *t2* (let ((table (make-hash-table :test #'eq))) (setf (gethash 'a table) *t1*) table)) (defun existe-phrase (phrase corpus) (if (endp phrase) (null corpus) (and corpus (multiple-value-bind (value found) (gethash (car phrase) corpus) (and found (existe-phrase (cdr phrase) value)))))) (defun corpus-to-list (corpus) (if (null corpus) '(()) (let ((phrases nil)) (maphash (lambda (key value) (loop for semiphrase in (corpus-to-list value) do (push (cons key semiphrase) phrases))) corpus) phrases)))