PDA

View Full Version : LISP Tutorial



Echo_
05-03-2012, 03:36 PM
I've been reading through these tutorials on the LISP programming language, and they are really good. They have exercises and stuff to make sure you understand what you are reading.

http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/1/tutorial1.html
http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/2/tutorial2.html
http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/3/tutorial3.html
http://www.cs.sfu.ca/CourseCentral/310/pwfong/Lisp/4/tutorial4.html

Here are my solutions so far :):

Tutorial 1:
(defun triangular (n)
"Computes the n'th triangular number."
(if (= n 1)
1
(+ n (triangular (1- n)))))

(defun power (b e)
"Computes b to the power of e."
(if (zerop e)
1
(* b (power b (1- e)))))

(defun binomial (n r)
"Computes the coefficient of the term x^r in the binomial expansion of (1 + x)^n."
(if (or (zerop r) (= r n))
1
(let
((x (binomial (1- n) (1- r)))
(y (binomial (1- n) r)))
(+ x y))))

(defun sum (x)
"Computes the sum of all the elements in list x."
(if (null x)
0
(+ (first x) (sum (rest x)))))

(defun list-last (x)
"Returns the last element in list x."
(cond
((null x) nil)
((= (list-length x) 1) x)
(t (list-last (rest x)))))

(defun list-butlast (x)
"Returns all but the last element of list x."
(cond
((null x) nil)
((= (list-length x) 2) (list (first x)))
(t (cons (first x) (list-butlast (rest x))))))

(defun list-union (x y)
"Returns a list containing elements from both list x and y."
(cond
((null x) y)
((not (member (first x) y)) (cons (first x) (list-union (rest x) y)))
(t (list-union (rest x) y))))

(defun list-difference (x y)
"Returns a list containing elements in list x that are not in list y."
(cond
((null x) nil)
((not (member (first x) y)) (cons (first x) (list-difference (rest x) y)))
(t (list-difference (rest x) y))))

Tutorial 2:
(defun triangular (n)
(defun triangular-aux (n a)
(if (= n 1)
a
(triangular-aux (1- n) (+ n a))))
(triangular-aux n 1))

(defun power (b e)
(defun power-aux (b e a)
(if (zerop e)
a
(power-aux b (1- e) (* b a))))
(power-aux b e 1))

(defun fast-list-length (x)
(defun list-length-aux (x a)
(if (null x)
a
(list-length-aux (rest x) (1+ a))))
(list-length-aux x 0))

(defun apply-func-list (f x)
(if (null f)
x
(funcall (first f) (apply-func-list (rest f) x))))

(defun f1 ()
(apply-func-list (list #'(lambda (a) (* a 10)) #'(lambda (a) (nth 3 a))) '(10 20 30 40 50)))

(defun f2 ()
(apply-func-list (list #'(lambda (a) (nth 2 a)) #'(lambda (a) (nth 1 a))) '((1 2) (3 4 5) (6))))

(defun f3 ()
(apply-func-list (list #'(lambda (a) (- 10 a)) #'list-length) '(a b c d e f)))

(defun f4 ()
(apply-func-list (list #'list #'(lambda (a) (cons 'blah a))) nil))

(defun find-null (x)
(if (null x)
nil
(if (consp (first x))
(first x)
(find-null (rest x)))))

(defun f5 (x)
(find-if #'(lambda (a) (= (list-length a) 3)) x))

(defun f6 (x)
(find-if #'(lambda (a) (evenp (list-length a))) x))

(defun f7 (x)
(find-if #'(lambda (a) (= (rem a 3) 0)) x))

(defun list-remove-if (f x)
(if (null x)
nil
(if (funcall f (first x))
(list-remove-if f (rest x))
(cons (first x) (list-remove-if f (rest x))))))

(defun list-difference (x y)
(list-remove-if #'(lambda (a) (member a y)) x))

(defun list-intersection (x y)
(remove-if-not #'(lambda (a) (member a y)) x))

(defun list-min-max (x)
(defun min-max-aux (x a b)
(cond
((null x) (values a b))
((> a (first x)) (min-max-aux (rest x) (first x) b))
((< b (first x)) (min-max-aux (rest x) a (first x)))
(t (min-max-aux (rest x) a b))))
(min-max-aux x (first x) (first x)))

DatSik
05-03-2012, 10:51 PM
Nice, thank you for the links. Havent read about lisp much, this provides good insight