Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.59 KB | None | 0 0
  1. (defun fact(n) ; Problema 1
  2.     (cond ((zerop n) 1)
  3.           ((equal n 1) 1)
  4.           (t (* n (fact (- n 1))))
  5.     )
  6. )
  7.  
  8. (defun fibonacci(n) ; Problema 2
  9.     (cond ((zerop n) 0)
  10.           ((equal n 1) 1)
  11.           (t (+ (fibonacci(- n 1)) (fibonacci(- n 2))))
  12.     )
  13. )
  14.  
  15. (defun my_member(x lst) ; Problema 3
  16.     (cond ((null lst) nil)
  17.           ((equal (car lst) x) t)
  18.           (t (my_member x (cdr lst)))
  19.     )
  20. )
  21.  
  22. (defun trim-head (lst n) ; Problema 4
  23.     (cond ((> n (length lst)) '(nil))
  24.           ((null lst) '(nil))
  25.           ((zerop n) lst)
  26.           (t (trim-head (cdr lst) (- n 1)))
  27.     )
  28. )
  29.  
  30. (defun trim-tail (lst n) ; Problema 5
  31.     (reverse (trim-head (reverse lst) n))
  32. )
  33.  
  34. (defun count-atoms (lst) ; Problema 6
  35.     (cond ((null lst) 0)
  36.           (t (+ (cond ((atom (car lst)) 1)
  37.                       ((count-atoms (car lst))))
  38.                (count-atoms (cdr lst))))
  39.     )
  40. )
  41.  
  42. (defun squash (lst) ; Problema 10
  43.     (cond ((null lst) nil)
  44.           ((atom lst) (list lst))
  45.           (t (append (squash (car lst)) (squash (cdr lst))))
  46.     )
  47. )
  48.  
  49. (defun presentp (n lst) ; Problema 9
  50.     (cond ((null lst) nil)
  51.           ((cond ((atom (car lst)) (equal (car lst) n))
  52.                  ((presentp n (car lst)))))
  53.           (t (presentp n (cdr lst)))
  54.     )
  55. )
  56.  
  57. (defun rev (lst a) ; Problema 8
  58.     (cond ((null lst) a)
  59.           (t (rev (cdr lst) (cons (car lst) a)))
  60.     )
  61. )
  62.  
  63. (defun my_reverse(lst) (rev lst nil))
  64.  
  65. (defun inc (n) (+ n 1)) ; Problema 7
  66. (defun my_add(n1 n2 ac)
  67.     (cond ((equal ac (+ n1 n2)) ac)
  68.           (t (my_add n1 n2 (inc ac)))
  69.     )
  70. )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement