lisp recitation (cse471/598 fall 2007 ) aravind kalavagattu

23
Lisp Recitation (cse471/598 Fall 2007) Aravind Kalavagattu

Post on 18-Dec-2015

217 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

Lisp Recitation (cse471/598 Fall 2007)

Aravind Kalavagattu

Page 2: 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

Page 3: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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)

Page 4: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

An example LISP program!

LISP in a box!

(defun helloworld()

(format t “hello world”))

Page 5: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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 NotationThe 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.

Page 6: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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)

Page 7: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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 !

Page 8: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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.

Page 9: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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}

Page 10: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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)

Page 11: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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

Page 12: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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

Page 13: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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)))))

Page 14: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

RECURSION

Compute factorial:

(defun factorial (n)

(if (= n 0)

1

(* n ( factorial (- n 1) ) ) ) )

Page 15: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

Input and Output

> (progn

(format t “Please enter your name: ”)

(read-line))

> (prin1 “hello”)

Note: There are many variants.

Page 16: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

(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

Page 17: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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

Page 18: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

Loading a lisp file into lisp-in-a-box (windows)

( load “<filename>”)The filename is the path with “\” escapedExample: (load "C:\\Documents and Settings\\Aravind\\Desktop\\code.lisp")

Page 19: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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

Page 20: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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

Page 21: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

How to deal with problems

TA We can discuss one-on-one during my office hours!

Where you can ask:1. Newsgroup: comp.lang.lisp2. Search Google.3. Check the documentation of specified

functions.4. Discuss with each other

Page 22: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

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

Page 23: Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu

“Lisp is a language for smart people.”

Have Fun with Lisp

Questions?