;; ----------- questao 1 ;; versao com dotimes (defun diag (M) (let (a) (dotimes (i (min (length M) (length (first M))) (reverse a)) (push (elt (pop M) i) a) ))) ;versao com loop (defun diag2 (M) (let (linha out (c 0)) (loop (setf linha (first M)) (setf M (rest M)) (setf out (append out (list (elt linha c)))) (setf c (1+ c)) (if (or (null M) (= c (length linha))) (return out)) ))) ;;------------ questao 2 ;; versao com mapcar (defun centro (l) (let ((n (apply #'+ (mapcar #'third l)))) (list (/ (apply #'+ (mapcar (lambda (x) (* (first x)(third x))) l)) n) (/ (apply #'+ (mapcar (lambda (x) (* (second x) (third x))) l)) n) ))) ;; versao com dolist (defun centro2 (l) (let ((xs 0) (yx 0) (ms 0) ) (dolist (p l) (setf xs (+ xs (first p))) (setf ys (+ ys (second p))) (setf ms (+ ms (third p))) ) (list (/ xs ms) (/ ys ms)) )) ;;------------ questao 3 ;; 3a (defun subconjn (c n) (if (= n 1) (mapcar #'list c) (let (out (cc c)) (dolist (x c out) (pop cc) (setf out (append out (mapcar (lambda (y) (cons x y) ) (subconjn cc (1- n))))) )) )) ;; 3a - um versao recursiva (defun subconj-r (c n) (cond ((= n 1) (mapcar #'list c)) ((= n (length c)) (list c)) (t (append (mapcar (lambda (x) (cons (first c) x)) (subconj-r c (- 1 n))) (subconj-r (rest c) n))) )) ;; 3b (defun prodcartn (c n) (if (= n 1) (mapcar #'list c) (let (out) (dolist (x c (reverse out)) (setf out (append out (mapcar (lambda (y) (cons x y)) (prodcartn c (1- n))))) )))) ;; 3c (defun subconj2 (c) (let (out) (dolist (x c) (pop c) (dolist (y c) (push (list x y) out) )) out)) ;; 3d (defun prodcart2 (c) (let (out) (dolist (x c out) (dolist (y c) (push (list x y) out) ))) )