Advertisement
Guest User

Untitled

a guest
Mar 14th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.05 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.           ((+ (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.           ((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. (setq lst '(1 4 5))
  43. (print (my_member 3 lst))
  44. (print (fibonacci 10))
  45. (print (trim-tail '(1 2 3 4 5) 2))
  46. (print (count-atoms '(a b ((a) c) e)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement