Top Banner
Common Lisp! John Paxton Montana State University Summer 2003
24

Common Lisp! John Paxton Montana State University Summer 2003.

Dec 16, 2015

Download

Documents

Fredrick Wythe
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Common Lisp! John Paxton Montana State University Summer 2003.

Common Lisp!

John Paxton

Montana State University

Summer 2003

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

Page 3: Common Lisp! John Paxton Montana State University Summer 2003.

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!

Page 4: Common Lisp! John Paxton Montana State University Summer 2003.

Material

• Defining Functions

• Local Variables

• Using Files

• Predicates

• Boolean Operands

• Lisp Representation

Page 5: Common Lisp! John Paxton Montana State University Summer 2003.

defun

(defun add-ten (n)

(+ n 10)

)

ADD-TEN

> (add-ten 7)

17

Page 6: Common Lisp! John Paxton Montana State University Summer 2003.

let

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

(+ a b)

)

3

> (+ a b)

- EVAL: variable A has no value

Page 7: Common Lisp! John Paxton Montana State University Summer 2003.

let*

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

(+ a b)

)

3

Page 8: Common Lisp! John Paxton Montana State University Summer 2003.

Using Files

• (load “file-name.l”)

• (load “file-name.lsp”)

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

• (load “file-name”)

Page 9: Common Lisp! John Paxton Montana State University Summer 2003.

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

Page 10: Common Lisp! John Paxton Montana State University Summer 2003.

Boolean Values

• t = any non-empty list

• nil = ‘() = ()

Page 11: Common Lisp! John Paxton Montana State University Summer 2003.

Equality Predicates

• = (= 3 4)

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

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

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

Page 12: Common Lisp! John Paxton Montana State University Summer 2003.

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

Page 13: Common Lisp! John Paxton Montana State University Summer 2003.

Other Predicates

• listp

• atom

• numberp

• symbolp

• null

• endp

• >, <, >=, <=

Page 14: Common Lisp! John Paxton Montana State University Summer 2003.

Boolean Operators

• and

• or

• not

Page 15: Common Lisp! John Paxton Montana State University Summer 2003.

if statement

(if (> 2 3) 'bigger)

NIL

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

NOT-BIGGER

Page 16: Common Lisp! John Paxton Montana State University Summer 2003.

cond statement

(cond

(test1 statement 1 … statement n)

(test2 statement 1 … statement m)

(testk statement 1 .. statement p)

)

Page 17: Common Lisp! John Paxton Montana State University Summer 2003.

cond statement

(cond

((> temperature 30) 'hot)

((> temperature 25) 'warm)

(t 'pleasant)

)

Page 18: Common Lisp! John Paxton Montana State University Summer 2003.

case statement

(case temperature

(35 ‘hot)

(34 ‘hot)

(otherwise ‘pleasant)

)

Page 19: Common Lisp! John Paxton Montana State University Summer 2003.

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.

Page 20: Common Lisp! John Paxton Montana State University Summer 2003.

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.

Page 21: Common Lisp! John Paxton Montana State University Summer 2003.

Underlying Representation

• ‘(1 2 3)

12 3

Page 22: Common Lisp! John Paxton Montana State University Summer 2003.

Underlying Representation

• ‘(1 (2) 3)

1 3

2

Page 23: Common Lisp! John Paxton Montana State University Summer 2003.

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

Page 24: Common Lisp! John Paxton Montana State University Summer 2003.

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)