introduction

40
Introduction CSC 358/458 3/27/2006

Upload: braima

Post on 06-Jan-2016

43 views

Category:

Documents


0 download

DESCRIPTION

Introduction. CSC 358/458 3/27/2006. Outline. Introductions Course goals Course organization History of Lisp Lisp syntax Examples Lisp IDE. Me. Robin Burke CST 453 [email protected] Programming in Lisp for about 20 years would program in nothing else if given the choice. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Introduction

Introduction

CSC 358/458

3/27/2006

Page 2: Introduction

Outline

Introductions Course goals Course organization History of Lisp Lisp syntax Examples Lisp IDE

Page 3: Introduction

Me

Robin Burke CST 453 [email protected] Programming in Lisp for about 20

yearswould program in nothing else if given

the choice

Page 4: Introduction

Resources

Course web site http://josquin.cti.depaul.edu/~rburke/courses/s06/

cs358/ • Homework / Lab assignments• Lecture notes

Currently off-line (hacker attack)• temporary substitute• http://condor.depaul.edu/~rburke4/courses/s06/cs358/

CTI Course on-line Course forum Homework submission Grades

Page 5: Introduction

Lisp environment

Allegro Common Lispfree download www.franz.comlabsterminal server

There are other implementations including open-source ones

Page 6: Introduction

Symbolic Programming

Programming as manipulation of symbols

Page 7: Introduction

Math without Symbols

... a square and 10 roots are equal to 39 units. The question therefore in this type of equation is about as follows: what is the square which combined with ten of its roots will give a sum total of 39? The manner of solving this type of equation is to take one-half of the roots just mentioned. Now the roots in the problem before us are 10. Therefore take 5, which multiplied by itself gives 25, an amount which you add to 39 giving 64. Having taken then the square root of this which is 8, subtract from it half the roots, 5 leaving 3. The number three therefore represents one root of this square, which itself, of course is 9. Nine therefore gives the square.

Page 8: Introduction

Math with Symbols

22

2b

cb

x

cbxx 2

Page 9: Introduction

Symbolic Programming

Symbolic representation of a problem Formal operations to manipulate the

representations Closely related to AI

Page 10: Introduction

Symbolic programming

Another important application of symbolic manipulation is game-playing

Game tokens (cards, pieces) are symbolic representations of game state

The rules of a game describe how these tokens are manipulated

Page 11: Introduction

Topics

Introduction Lists and Trees Functions Strings and I/O Midterm Control structures AI application: pattern matching Macros AI application: search

Page 12: Introduction

Lisp origins

2nd Oldest Programming LanguageJohn McCarthy, MIT 1960s

Originally intended as an internal language for working with FORTRAN programsdeveloped a life of its own

LISt Processing

Page 13: Introduction

Lisp History

Early implementationsvery slowpurely interpretedwidely varying semanticsdifferent function libraries

80sAI boomneed for portable LispCommonLisp initiative

Page 14: Introduction

Lisp Today

Many Lisp ideas become mainstreamprototypingdynamic memory managementfunctions as first-class objects

Modern Lispvery fastmulti-paradigm programmingextensible

Page 15: Introduction

Why you should study Lisp

Because it is weird unusual syntax unusual computational model unusual development approach

Because it is extremely powerful some aspects of Lisp do not exist in any mainstream

language Lisp can make programming incredibly efficient Lisp can express some algorithmic ideas more directly than

other languages Because you may use it

commercial applications (including autoCAD and Yahoo MyStore)

AI applications and research tools

Page 16: Introduction

Syntax

Basic elements of the language Relationships between them

Page 17: Introduction

Essentials of Lisp syntax

Evaluation everything has a value computation = evaluating that value

Example running a program >(myprogram)

What am I doing? Asking Lisp to evaluate an expression Because the expression is in parentheses

• it is assumed to be function call The symbol "myprogram" is evaluated

• used to look up a previously-defined function This function is called What the function returns is printed out

Page 18: Introduction

Lisp syntax 2

More examples > 2 > (+ 2 3) > (* a b)

2 has a value in and of itself (+ 2 3)

is a function call + evaluates to the addition operator 2 and 3 evaluate to themselves the addition operator is called with 2 and 3 as arguments 5 is returned

(* a b) * evaluates to the multiplication operator a and b are evaluated

• presumably they are variables with values whatever their values are get multiplied together

Page 19: Introduction

Lisp syntax 3

Every expression is eithera list

• (a b c)

an atom • 5

Atoms are evaluatedsome self-evaluatesymbols look for bindings

Page 20: Introduction

Lisp syntax 4

Liststhe first element is evaluated as a

functioneach other element is evaluatedthen the function is called with the

other elements as argumentsa value is returned

Page 21: Introduction

Exception

If first element is the name of a special form or macrothen all arguments may not be

evaluated Example

(if (and (< health 5) (< enemy-distance 10)) (self-destruct) (fire-weapon))

Page 22: Introduction

Evaluation

a pi “a” #\a (cos pi) ‘(cos pi)

Page 23: Introduction

Note on types

Lisp has a very elaborate (user-extensible) type system Every value in Lisp has a type

but variables do not have types You do not have to tell Lisp what type a variable will hold

because it doesn't matter to the interpreter (Sigh of relief from the C++ programmers) if you want / need to you can give this information to the

compiler• and your code may be more efficient• but only if you need to

That doesn't mean Lisp doesn't care about types it doesn't allow crazy type conversions like C if you try to do something illegal, you'll get a run-time error

• not a compile-time error

Page 24: Introduction

Quote

Prevents evaluationcrucial for symbols

Obviously a special form (quote a) => a Abbreviation

' 'a => a

Page 25: Introduction

Functional values

Different kind of quoting(function tan) => #<Function TAN>

Abbreviation#'#'tan => #<Function TAN>

We can pass functions as arguments(funcall #'tan 1) equivalent to(tan 1)

Page 26: Introduction

Lists

Basic Lisp data type (a b c)

Front a "car"

Rest (b c) "cdr"

nil = empty list

Page 27: Introduction

Lists, cont'd

Assembling lists(list 'a 'b 'c)(cons 'a '(b c))(append '(a) '(b c))

Lists can contain lists(a (b c) (d e (f)))

Lisp functions are lists!(defun isDead (enemy)

(<= (enemy-health enemy) 0))

Page 28: Introduction

Lists, cont'd

We can treat lists like arrays (nth 2 '(2 4 8 16 32)) 8

We can treat lists like stacks (setq lst '(a b c)) (pop lst)

• returns a• lst now contains (b c)

(push 'q lst)• lst now contains (q b c)

We can even treat lists like tables association lists (assoc 'j '((k 13) (q 12) (j 11))

• returns (j 11)• the pair whose car matches the symbol j

Page 29: Introduction

Assignment

Bind a symbol to a value(setf a '(1 2 3))

Also(set 'a '(1 2 3))then (setq a '(1 2 3))

Setf generalizes setqfirst argument can be a function call(setf (car l) 0)

Page 30: Introduction

Defining functions

(defun name (args) body) Examples

(defun square (x) (* x x))(defun sum-sq (x y) (+ (* x x) (* y y)))

Recursive functionfactorial

Page 31: Introduction

Truth values

T/nilactually any non-nil value is assumed

to be T

Page 32: Introduction

Conditions

Syntax(if exp1 exp2 [exp3])

If exp1 evaluates to Tevaluate exp2 and return the valueotherwise evaluate exp3 and return itif no exp3, return nil

Page 33: Introduction

And / Or / Not

(and exp1 exp2 ... )keep evaluating while T

(or exp1 exp2 ... )keep evaluating while nil

(not exp1)reverse truth value

Page 34: Introduction

Cond

Generalized conditional(cond clause1 ... clausen)clause = (condition value)

Example(defun interest-rate (money)

(cond ((< money 0) 0)

((< money 1000) 2)

((< money 10000) 5)

((< money 100000) 7)

(t 10)))

Page 35: Introduction

Equality

Equality is kind of complicated we’ll return to this topic

Different operators =

• for numbers eq

• for identicality (same pointer) equal

• for “looks the same”• most expensive to compute

Page 36: Introduction

Let

Defining local variables(let ((var1 val1) .. (varn valn))

body) What is the value?

the value of the last expression of the form Example

(defun fielding (put-outs assists errors)(let ((numerator (+ put-outs assists))

(denom (+ put-outs assists errors))) (/ numerator denom)))

Page 37: Introduction

Let*

Same syntax but assignments made sequentially Example

(defun fielding (put-outs assists errors)(let* ((numerator (+ put-outs assists))

(denom (+ numerator errors)))

(/ numerator denom)))

Page 38: Introduction

Simple iteration

(We'll see many more forms in this class)

Do something once for each item in a list(dolist (var list)

.. body) Do something k times

(dotimes (var k).. body)

Page 39: Introduction

Extended Example

cards

Page 40: Introduction

Break

Lab CST 6th floor