All pastes #25896 Raw Edit

exercicio

public text v1 · immutable
#25896 ·published 2005-10-18 22:27 UTC
rendered paste body
(defun ~(x)
  (if x NIL T)
 )

(defun V(x y)
  (if x T (if y T NIL))
)

(defun A(x y)
  (if x y NIL)
  
 )
 
 (defun xor(x y)

   (if x (~ y) y)
   
   )

(defun nor(x y)

  (~ (V x y))

)

(defun nand(x y)

  (~ (A x y))

  )

(defun imp(x y)

  (if x y T)
  )

(defun iff(x y)

  (~ (xor x y)

  )
  
  )

(defun maior(x y)
  (cond ((zerop x) nil)
	((zerop y) T)
	(T (maior(1- x) (1- y)))
	)
  )

(defun igual(x y)
  (if (zerop x) (if(zerop y) T nil) (igual(1- x) (1- y)))
 )

(defun menor(x y)
  (cond ((zerop y) nil)
	((zerop x) T)
	(T (menor(1- x) (1- y)))
	)
  )


(defun somar(x y)
  (cond ((zerop x) y)
	((zerop y) x)
	(T (somar(1+ x)(1- y)))
   )
  )

(defun subtrair(x y)
  (if (zerop y) x (1- (subtrair x (1- y))))
  )

(defun negativo(x y)
  (cond ((zerop x) T)
	((zerop y) nil)
      (T (negativo(1+ x)(1- y)))
  )
)

(defun positivo(x y)
  (cond ((zerop x) T)
	((zerop y) nil)
	(T (positivo(1- x)(1+ y)))))
     
(defun simetrico(x)
  (- (- x x) x)
  )


(defun multiplicar(x y)
  (cond ((zerop (1- y)) x)
	(T (multiplicar (somar x x) (1- y)))))

(defun multiplicar2(x y)
  (cond ((zerop x) x)
	((zerop y) x)
	(T (1- (1- y))
	   (if(zerop y) (1+ x) (multiplicar2 (1+ (1+ x))(1- y))))))


;Problema 4
(defun n-esimo(x L)
  (if (null L) nil
      (if(zerop x) (first L) (n-esimo (1- x) (rest L)))))
      

(defun comprimento(L)
  (if (null L) 0
      (1+ (comprimento(rest L)))))


(defun profundidade(L)
   (cond
     ((null L) 0)
     ((atom(first L)) (max 1 (profundidade(rest L))))
     (T (max(1+ (profundidade(first L)))
	(1+ (profundidade(rest L))))
     )
   )
)


(defun junta(L1 L2)
  (if (null L1) L2
      (if(null L2) L1
	 (cons(first L1) (junta (rest L1) L2))
       )
   )
)


(defun inverte(L)
  (cond ((null L) nil)
	(T (junta (inverte(rest L)) (list(first L))))
	)
)


(defun numatomos(L)
  (cond
     ((null L) 0)
     (T (if (atom (first L))
	    (1+ (numatomos(rest L)))
	    (numatomos(rest L))))))

(defun existe(x L)
  (if (null L) nil
      (if (= (first L) x)
	  (cons (first L) (rest L))
	  (existe x (rest L))
      )
  )
)
	
;retorna a lista com todos os elementos na lista principal
(defun alisa(L)
  (if (null L) nil
      (if (listp (first L))
	  (append (alisa (first L)) (alisa (rest L)))
	  (cons (first L) (alisa (rest L)))
      )
   )
)


;ordenar



;/////////////// PROBLEMA 4 \\\\\\\\\\\\\\\\\\\


;estruturas de dados para polinomios

 
 (defun somaListas(L1 L2)
   (if (null L1) L2
       (if (null L2) L1
	   (cons (+ (first L1) (first L2)) (somaListas(rest L1) (rest L2)))
       )
    )
  )
	    
 (defun subtraiListas(L1 L2)
       (if (null L2) L1
	    (if (null L1) (cons (- 0 (first L2)) (subtraiListas L1 (rest L2)))
	   (cons (- (first L1) (first L2)) (subtraiListas(rest L1) (rest L2)))
       )
    )
  )

(defun multiplicaListas(L1 L2)
  (if (null L2) NIL
      (if (null L1) NIL
	  (somaListas (mapcar #'(lambda(x) (* x (first L1))) L2) (multiplicaListas (rest L1) (cons 0 L2)))
      )
  )
)
	  
	  	  
(defun derivaLista(L1 grau)
  (if (null L1) NIL
      (if(zerop grau) NIL
	 (cons (* (first L1) grau) (derivaLista (rest L1) (1- grau)))
      )

  )
)      


;polinomios

(defstruct poli o pl)


(defun somaPolinomio(p1 p2)
  ( make-poli
     :o (max (poli-o p1) (poli-o p2))
     :pl (somaListas (poli-pl p1) (poli-pl p2))
  )
)

(defun subtrairPolinomio(p1 p2)
  ( make-poli
     :o (max (poli-o p1) (poli-o p2))
     :pl (subtraiListas (poli-pl p1) (poli-pl p2))
  )
)

(defun multiplicaPolinomio(p1 p2)
  ( make-poli
     :o (max (poli-o p1) (poli-o p2))
     :pl (multiplicaListas (poli-pl p1) (poli-pl p2))
  )
)

(defun derivaPolinomio(p1)
  ( make-poli
     :o (1- (poli-o p1))
     :pl (derivaLista (poli-pl p1) (poli-o p1))
  )
)


(defun criarDasRaizes (a b)
    (let ((L1) (L2))
      (setf L1 (list (simetrico a) 1))
      (setf L2 (list (simetrico b) 1))
      (make-poli
      :o 2
      :pl (multiplicaListas L1 L2))
      
;      (multiplicaPolinomio #s(poli :o 1 :pl ((- 0 a) 1)) #s(poli :o 1 :pl ((- 0 b) 1)))
      
    )
)


(defun avaliar(p1 x1)
  (if (null (poli-pl p1)) 0
      (if (numberp (poli-pl p1)) (poli-pl p1)
	  (+ (* (first (reverse (poli-pl p1))) (verificaExpoente x1 (poli-o p1)))
	     (avaliar (make-poli :o (1- (poli-o p1))  :pl (reverse (rest (reverse (poli-pl p1))))) 2)))))


(defun verificaExpoente(x1 grau)
  (if (= grau 0) 1
      (if (= grau 1) x1
	  (* x1 (verificaExpoente x1 (1- grau))
           )
       )
   )
)