230 likes | 342 Views
Lisp Recitation (cse471/598 Fall 2007 ). Aravind Kalavagattu. Outline:. Setting up the environment LISP-in-a-box LISP Datatypes Expressions Functions Conditionals Looping Others CSE 471.. LISP. Functional Programming.
E N D
Lisp Recitation(cse471/598 Fall 2007) Aravind Kalavagattu
Outline: • Setting up the environment • LISP-in-a-box • LISP • Datatypes • Expressions • Functions • Conditionals • Looping • Others • CSE 471.. LISP
Functional Programming • Function is the basic element. Both data and function are treat as object. The parameter could be a function. • Basic rule: the parameters of a function should not be changed. (But there are some exceptions like sort, incf, decf)
An example LISP program! LISP in a box! (defun helloworld() (format t “hello world”))
Expression Evaluation • You type an expression, the interpreter responds by displaying the results of the evaluation of that expression. Ex. >486 The interpreter will respond 486 • Prefix Notation The leftmost element in the list is the operator and the other elements are operands. >(+ 137 349) 486 • Compound expressions are formed by combining other expressions. >(/ (- 7 1) (- 4 2)) 3 ** The way we call functions also is a ‘Prefix notation’ with function name followed by the arguments.
Data • Atoms • Numbers • >1 • >334 • Symbols • a • Horse • Variables, Arrays, Vectors, Structures • (make-array ‘(2 3)) • (aref <arrayname>) • (defstruct employee age first-name last-name sex children) EMPLOYEE • Lists • (1 2 3) • (7 a horse) • QUOTE operator • Takes a single argument, and returns it verbatim • > (quote (+ 3 5)) (+ 3 5)
Data Structure - Lists • Using Quote:- • ‘(1 2 3) also (quote ( 1 2 3)) • (1 2 3) • Using CAR:- • (CAR ‘( 1 2 3 4)) • 1 • Using CDR:- • (CDR ‘( 1 2 3)) • ( 2 3) • - cadr, cadddr, • nthcdr !
Using ‘list’: (list 'a '(a s d f)) • List takes all its arguments and makes a list with them as elements • Using Cons :- (cons 0 ‘(1 2 3)) adds the element 0 to the head of the list (1 2 3) (0 1 2 3) • Using Append :– (append ‘(1 2) ‘(3 4 5)) Creates a new list out of existing lists (1 2 3 4 5) List can also be interpreted as sets, hashtables, trees, sequences, stacks, association-lists.
Variables • Assignment • (setf x (list ‘a ‘b ‘c)) (A B C) • (setf (car x) ‘n) N • (setf a 1 b 2 c 3) { a=1; b=2; c=3}
Variables • Local: • (let ( ( < assg1 >) (<assg2>) … (<assg n>) ) (< Body >) ) Example: (let ((a 1) (b 3)) (+ 33 a b)) >> 37 • Global: • (defparameter *pi* 3.14) • (defconstant +limit+ 100)
Function • (defun (<args>) <body> ) Example: • (defun square (x) (* x x)) • SQUARE > (square 2) • 4 • > (square 1.4142158) • 2.0000063289696399 • Symbolp, listp • Truth and False values • (null nil) • true
Conditionals • Comparisons: >=, <=, eql, eq • If (if <test> <then> <else> ) Ex: (defun absdiff (x y) (if (> x y) (- x y) (- y x) )) • Cond (like “switch” in C/C++) (cond (A B) (C D) (t E)) (t E) is the ‘default’ equivalent Example: (defun absdiff (x y) (cond ((> x y) (- x y)) (t (- y x)))) ** Others: when, until, always
Looping • (dolist (<next-element> <target-list> <result> ) <body> ) • (dotimes (<counter> <limit> <result> ) <body> ) • Examples: • (defun num-sublists-i (lis) (let ((result 0)) (dolist (next lis result) (if (listp next) (setf result (1+ result)))))) • (defun power-i (x y) (let ((result 1)) (dotimes (count y result) (setf result (* x result)))))
RECURSION Compute factorial: (defun factorial (n) (if (= n 0) 1 (* n ( factorial (- n 1) ) ) ) )
Input and Output > (progn (format t “Please enter your name: ”) (read-line)) > (prin1 “hello”) Note: There are many variants.
(defun f-to-c (ftemp) (let ((ctemp (* (- ftemp 32) 5/9))) (format t "~%~s degrees Fahrenheit is ~%~s degrees Celsius~%" ftemp ;; first ~s (float ctemp)) ;; second ~s ctemp)) ;; return ratio value - ;; Commenting
Other Imp. LISP stuff • Mapcar • The MAPCAR form is an "iterator" that applies a function repeatedly, to each element of a list and returns a list of the results. For example: > (MAPCAR 'ATOM '(DOG (CAT HORSE) FISH)) (T NIL T) • Apply • Apply takes its first argument and applies it as a function to the list of items making up the last. For example: >(apply '* '(1 2 3 4)) 24 • Lambda • You can think of a lambda expression as an anonymous function. • > (setf product '(lambda (x y) (* x y))) (LAMBDA (X Y) (* X Y)) • > product (LAMBDA (X Y) (* X Y)) • > (apply product '(3 4)) 12
Loading a lisp file into lisp-in-a-box (windows) • ( load “<filename>”) • The filename is the path with “\” escaped • Example: (load "C:\\Documents and Settings\\Aravind\\Desktop\\code.lisp")
Suggested programming style • Write short functions, where each function provides a single, well-defined operation • Use proper indentation • Program idea with recursion • Top-down approach with abstraction
Useful commands for lisp-in-a-box / emacs • Ctrl-c Ctrl-d; Ask for the description • Alt-p; Run through the history • Ctrl-c Ctrl-q; complete all the parenthesis • Ctrl-c Ctrl-z; Return to the interpreter • Ctrl-c Ctrl-c; Compile a function • Ctrl-c Ctrl-k; Compile and load a file • Other basic commands for emacs
How to deal with problems • TA • We can discuss one-on-one during my office hours! • Where you can ask: • Newsgroup: comp.lang.lisp • Search Google. • Check the documentation of specified functions. • Discuss with each other
CSE 471 .. • What kind of projects (LISP)? • Partial code is given many times • Understand its working and extend for tasks in the project • Project 0? • Finish a good full tutorial on LISP and start the project • LISP Primer • Common Lisp tutorials • Use LISP references whenever needed
“Lisp is a language for smart people.”Have Fun with Lisp Questions?