Skip to content

Instantly share code, notes, and snippets.

@oxitnik
Created April 8, 2012 07:38
Show Gist options
  • Save oxitnik/2335708 to your computer and use it in GitHub Desktop.
Save oxitnik/2335708 to your computer and use it in GitHub Desktop.
lisp lab 3
;1
(defun fib-r (n)
(cond
( (eql n 1) 1)
( (eql n 2) 2)
(t (+ (fib-r (- n 1)) (fib-r (- n 2))))
)
)
(defun fib-i (n)
(do ((i n (- i 1))
(old 1)
(new)
(result 1))
((< i 2) result)
(setf new (+ result old))
(setf old result)
(setf result new)
))
;2
(defun len-r (lst)
(if (NULL lst)
0
(+ (len-r (cdr lst)) 1)
)
)
;3
(defun sqr-sum (m n )
(do ((i m (+ i 1)) (s 0 (+ s (* i i)))) ((= (+ n 1) i) s))
)
;4
(defun gcd-r (a b)
(if (= b 0) a (gcd-r b (mod a b))))
(defun lcm-r (a b)
(/ (abs (* a b)) (gcd-r a b))
)
;5
(defun num-sum (lst)
(if (numberp lst) lst (if (not (atom lst)) (+ (num-sum (car lst)) (num-sum (cdr lst))) 0) )
)
;6
(defun rm-item (itm lst)
(if (not (null lst))
(if (not(equal itm (car lst)))
(cons (car lst) (rm-item itm (cdr lst)) )
(rm-item itm (cdr lst))
)
()
)
)
;7
(defun item-count (itm lst)
(if (not (null lst))
(if (equal itm (car lst))
(+ 1 (item-count itm (cdr lst)) )
(item-count itm (cdr lst))
)
0
)
)
(defun simp-list (lst lst2)
(if (atom lst)
(if (null lst) lst2 (cons lst lst2))
(append (simp-list (car lst) lst2) (simp-list (cdr lst) lst2) )
)
)
;8
(defun list-max (lst)
(apply 'max (simp-list lst ()))
)
;9
(defun reverse-all (lst)
(if (null lst)
nil
(append (reverse-all(cdr lst)) (list (if (listp (car lst)) (reverse-all (car lst)) (car lst) ) ))
)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment