;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; CPSC 625 and 420 ;; ;; Exercises: Some (rather) random Lisp expressions ;; ;; Yoonsuck Choe ;; http://faculty.cs.tamu.edu ;; Thu Sep 2 10:52:37 CDT 2004 ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; various list operations (car '(a (b c))) (car '((b c) a)) (cdr '(a (b c))) (cdr '((b c) a)) ; various list operations (cadr '(a (b c))) (list 'a '(1 2) '((3 5) (7 8))) (length '(a b c)) (first '(a b)) (nth 2 '(a b c d)) ; various list operations (cons 'a '(1 2 3)) (cons '(a) '(1 2 3)) ; assignment (setq x 10) x ; assignment (setq a (make-array '(3 3))) (aref a 2 2) (setf (aref a 2 2) 1000) (setq (aref a 2 2) 1000) ; assignment (into address) (setf b '(1 (2 3) 4)) (caadr b) (setf (caadr b) 'abcdefg) b ; arithmetic (+ (* 2 3) (/ 4 5)) ; function (defun mult (x y) (* x y) ) (defun mult (x y) (let ((tx x) (ty y)) (* tx ty) ) ) ; recursion (defun fibo (x) (cond ((equal x 1) 1) ((equal x 2) 2) ((> x 2) (+ (fibo (- x 1)) (fibo (- x 2)))) ) ) (defun fibo (x) (print (format nil "Entering fibo(~d)~%" x)) (cond ((equal x 1) 1) ((equal x 2) 2) ((> x 2) (+ (fibo (- x 1)) (fibo (- x 2)))) ) ) ; local context (let ((a 3)) (+ a 1)) (let ((a 2) (b 3) (c 0)) (setq c (+ a b)) c ) (setq c 4) (let ((c 5)) c) c ; lexical scope (setq ext:*top-level-auto-declare* nil) (setq regular 5) (defun check-regular () regular) (check-regular) (let ((regular 6)) (check-regular)) ; dynamic scope (defvar *special* 5) (defun check-special () *special*) (check-special) (let ((*special* 6)) (check-special)) *special* (let ((x 23)) (check-special)) ; blocks (progn (setq a 123) (* 5 10) ) ; errorneous block ( (setq x 10) (setq y 20) (* x y) ) ; conditionals (if (> 2 3) ; condition (+ 4 5) ; when true (* 4 5) ; when false ) (defun compare-it (x) (cond ((eq x 1) (* x x)) ((eq x 2) (+ x x)) ((eq x 3) (/ x 2)) (T 0) ) ) (compare-it 1) (compare-it 2) (compare-it 3) (compare-it 50) (numberp 10) (zerop 2) (atom 'x) (atom '(1 2 3)) ; loops (dotimes (k 10 val) (setq val k)) ; formatted output (format t "Hello, world!") (format nil "Hello, world!") (format nil "One: ~d~%Two:~f~%Three:~5,2f" 12 (/ 4 3) (/ 4 3) )