common lisp! john paxton montana state university summer 2003

24
Common Lisp! John Paxton Montana State University Summer 2003

Upload: fredrick-wythe

Post on 16-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

Common Lisp!

John Paxton

Montana State University

Summer 2003

Montana Statistics

• 2001 Population: 904,433. In contrast, El Salvador has a population of 6,353,681.

• 90.6% White• 6.2% Native American• 2.0% Hispanic or Latino

• Bozeman Population: 27,509.

Montana Statistics

• Land Area: 376,980 square kilometers. In contrast, El Salvador has 21,040 square kilometers.

• Fun fact: The greatest recorded 24 hour temperature change occurred in Loma, Montana in 1972. The temperature rose from -49 F to 54 F!

Material

• Defining Functions

• Local Variables

• Using Files

• Predicates

• Boolean Operands

• Lisp Representation

defun

(defun add-ten (n)

(+ n 10)

)

ADD-TEN

> (add-ten 7)

17

let

> (let ((a 1)(b 2))

(+ a b)

)

3

> (+ a b)

- EVAL: variable A has no value

let*

> (let* ((a 1)(b (+ a 1)))

(+ a b)

)

3

Using Files

• (load “file-name.l”)

• (load “file-name.lsp”)

• (compile-file “file-name.l”)

• (load “file-name”)

Questions

1. Write a function called gringo-dictionary that finds the spanish equivalent for an english word passed in.

2. Write a function that returns the roots of a quadratic equation in a list. For example, (roots 1 5 6) might return ‘(-2 -3).

Boolean Values

• t = any non-empty list

• nil = ‘() = ()

Equality Predicates

• = (= 3 4)

• eq (eq ‘(1 2) ‘(1 2))

(setf a ‘(1 2))(eq a a)

• equal (equal ‘(1 2) ‘(1 2))

member Predicate

(member '(1 2) '((1 1)(1 2)(1 3)))

NIL

> (member '(1 2) '((1 1)(1 2)(1 3)) :test #'equal)

((1 2) (1 3))

Other Predicates

• listp

• atom

• numberp

• symbolp

• null

• endp

• >, <, >=, <=

Boolean Operators

• and

• or

• not

if statement

(if (> 2 3) 'bigger)

NIL

> (if (> 2 3) 'bigger 'not-bigger)

NOT-BIGGER

cond statement

(cond

(test1 statement 1 … statement n)

(test2 statement 1 … statement m)

(testk statement 1 .. statement p)

)

cond statement

(cond

((> temperature 30) 'hot)

((> temperature 25) 'warm)

(t 'pleasant)

)

case statement

(case temperature

(35 ‘hot)

(34 ‘hot)

(otherwise ‘pleasant)

)

Questions

1. Write a function called blackjack that receives two card names as inputs. If one card is an ace and the other is a jack, the function should return the message blackjack. For example, (blackjack ‘ace ‘jack) would return ‘blackjack.

Questions

2. Define a function that shows a practical use of a cond statement.

3. Define a function that shows a practical use of a case statement.

4. Define a function that shows a practical use of nested conditional statements.

Underlying Representation

• ‘(1 2 3)

12 3

Underlying Representation

• ‘(1 (2) 3)

1 3

2

Questions

1. Draw the representation of ‘(1 (2 3 (4)) 5)

2. Draw the representation of ‘(defun add-one (n) (+ n 1))

3. Depict what happens below(setf list1 ‘(2 3))(setf list2 ‘(cons 1 list1))

Questions

4. Depict what happens below(setf list1 ‘(1 2))(setf list2 ‘(3))(setf list3 (append list1 list2))

5. Depict what happens below(setf alist ‘(1 2 3))(setf (second alist) 4)