Aula 8

1 Transposta

Soluçao da aula passada

;; da aula passada
(defun trans1 (mat)
    (if (null (first mat)) nil
        (cons (primeiros mat) (trans1 (restos mat)))
        ))

(defun primeiros (mat)
    (if (null (first mat)) nil
        (cons (first (first mat)) 
              (primeiros (rest mat)))
   ))

(defun restos (mat)
    (if (null (first mat)) nil
        (cons (rest (first mat)) (restos (rest mat)))
        ))

(print (trans1 '(( 1 2 3) (10 20 20) (90 91 92))))

Mas primeiros e restos podem ser mais facilmente implementados usando mapcar

(defun primeiros (mat)
  (mapcar #'first mat))

(defun restos (mat)
  (mapcar #'rest mat))

Uma solução fabulosa!!

(defun trans2 (mat)
   (apply #'mapcar #'list mat))

(print (trans2 '(( 1 2 3) (10 20 20) (90 91 92))))

2 Diagonal principal

(defun diag (mat)
    (if (null (first mat)) nil
        (cons (first (first mat))
              (diag (restos (rest mat)))
              )))

(print (diag '(( 1 2 3) (10 20 20) (90 91 92))))
(defun nesimo (l n)
     (if (= n 1) (first l)
         (nesimo (rest l) (- n 1))
))

(defun conta (n)
    (contax 1 n))

(defun contax (x n)
   (if (= n x) nil
      (cons x (conta (+ 1 x) n))
      ))

(defun diag2 (mat)
    (mapcar #'nesimo mat (conta (length mat)))
    )

(print (diag2 '(( 1 2 3) (10 20 20) (90 91 92))))

3 multiplicação de matrizes

(defun multmat (m1 m2)
  (let ((m2t  (trans1 m2))
       )
       (mapcar #'(lambda (l) (aux l m2t)) m1)
       ))

(defun aux (l m)
   (mapcar #'(lambda (x) (prodvet l x)) m))

(defun prodvet (l1 l2)
   (apply #'+ (mapcar #'* l1 l2)))

(print (multmat '((1 2 3) (4 5 6)) '((9) (8) (7)) ))

Author: Jacques Wainer

Created: 2018-03-21 Wed 09:43

Validate