# avolkov/lisp_examples

### Subversion checkout URL

You can clone with HTTPS or Subversion.

Fetching contributors…

Cannot retrieve contributors at this time

119 lines (114 sloc) 3.476 kb
 ; ex 1: Compute power of two for a given value (defun power-of-two (val) (do ((cur val (1- cur)) (res 2 (* 2 res))) ((= cur 1) res) ) ) (print (power-of-two 1)) (print (power-of-two 2)) (print (power-of-two 3)) (print (power-of-two 5)) (print (power-of-two 10)) (print (power-of-two 20)) (print "------------") ;ex 2: remove all occurences of an element 'el' from a list (defun it-remove (el val) (reverse (do ((cur val (cdr cur)) (res nil (cond ((not (equal el (car cur))) (cons (car cur) res))))) ((null cur) res) )) ) (print (it-remove 'a '())) (print (it-remove 'a '(a))) (print (it-remove 'a '(b c))) (print (it-remove 'a '(a b c))) (print "------------") (defun it-remove1 (el val) (do ((cur val (cdr cur)) (res nil (cond ((not (equal el (car cur))) (append res (list (car cur))))))) ((null cur) res) ) ) (print (it-remove1 'a '())) (print (it-remove1 'a '(a))) (print (it-remove1 'a '(b c))) (print (it-remove1 'a '(a b c))) (print "------------") ;ex 3: look up value in the list of list ;i.e. (it-assoc 'y '((a b) (c d) (e y))) --> (e, y) (defun it-assoc (el val) (do ((cur val (cdr cur)) (res nil (cond ((equal el (caar cur)) (append (car cur) res))))) ( (or (not ( null res)) (null cur)) res) ) ) (print (it-assoc 'y '((y a)))) (print (it-assoc 'y '(()))) (print (it-assoc 'y '((a b) (b c) (c d)))) (print (it-assoc 'y '((a b) (b c) (c d)(y f)))) (print "------------") ;ex 4: compute factorial (defun it-fact (num) (do ((cur num (1- cur)) (res 1 (* res cur))) ((= cur 1) res) ) ) (print (it-fact 1)) (print (it-fact 2)) (print (it-fact 3)) (print (it-fact 4)) (print (it-fact 10)) (print (it-fact 100)) (print "------------") ;ex 5: write a version of zip function in python (defun it-pairlis (l1 l2) (do ((cur (list l1 l2) (list (cdar cur) (cdadr cur))) (res nil (append res (list (list (caar cur)(caadr cur))) ) )) ((or (null (car cur)) (null (cadr cur))) res) ) ) (print (it-pairlis '() '())) (print (it-pairlis '(a b) '(1 2) )) (print (it-pairlis '(a b c) '(1 2) )) (print (it-pairlis '(a b c) '(1 2 3 4) )) (print (it-pairlis '(a b c) '() )) (print "------------") ;ex 6: find all perfect numbers in the range between these two numbers ;define function that finds a perfect number for a given n (defun perf-n (n) ;http://mathforum.org/dr.math/faq/faq.perfect.html (* (expt 2 (1- n)) (1- (expt 2 n))) ) (defun perf-range (nmin nmax) (reverse (do ((cur 0 (1+ cur)) (res nil (cond ( (>= (perf-n cur) nmin) (cons (perf-n cur) res)) ))) ((>= (perf-n cur) nmax) res) ) ) ) (print (perf-range 1 2)) (print (perf-range 1 100)) (print (perf-range 1 1000)) (print (perf-range 100 1000)) (print "------------") ;ex 7: Find an intersection of two lists (defun it-intersect (l1 l2) (do ((cur l1 (cdr cur)) (res '() (cond ((member (car cur) (cdr cur)) res) ((member (car cur) l2) (append res (list (car cur)))) (t res) ) ) ) ((null cur) res) ) ) (print (it-intersect '() '(a c b d e))) (print (it-intersect '(a b) '(a))) (print (it-intersect '(a a) '(a a))) (print (it-intersect '(a b) '(a c b d e))) (print (it-intersect '(a a) '(a c b d e))) (print (it-intersect '(a) '(a c b d e)))
Something went wrong with that request. Please try again.