introduction
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 PresentationTRANSCRIPT
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
yearswould program in nothing else if given
the choice
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
Lisp environment
Allegro Common Lispfree download www.franz.comlabsterminal server
There are other implementations including open-source ones
Symbolic Programming
Programming as manipulation of symbols
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.
Math with Symbols
22
2b
cb
x
cbxx 2
Symbolic Programming
Symbolic representation of a problem Formal operations to manipulate the
representations Closely related to AI
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
Topics
Introduction Lists and Trees Functions Strings and I/O Midterm Control structures AI application: pattern matching Macros AI application: search
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
Lisp History
Early implementationsvery slowpurely interpretedwidely varying semanticsdifferent function libraries
80sAI boomneed for portable LispCommonLisp initiative
Lisp Today
Many Lisp ideas become mainstreamprototypingdynamic memory managementfunctions as first-class objects
Modern Lispvery fastmulti-paradigm programmingextensible
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
Syntax
Basic elements of the language Relationships between them
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
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
Lisp syntax 3
Every expression is eithera list
• (a b c)
an atom • 5
Atoms are evaluatedsome self-evaluatesymbols look for bindings
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
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))
Evaluation
a pi “a” #\a (cos pi) ‘(cos pi)
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
Quote
Prevents evaluationcrucial for symbols
Obviously a special form (quote a) => a Abbreviation
' 'a => a
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)
Lists
Basic Lisp data type (a b c)
Front a "car"
Rest (b c) "cdr"
nil = empty list
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))
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
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)
Defining functions
(defun name (args) body) Examples
(defun square (x) (* x x))(defun sum-sq (x y) (+ (* x x) (* y y)))
Recursive functionfactorial
Truth values
T/nilactually any non-nil value is assumed
to be T
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
And / Or / Not
(and exp1 exp2 ... )keep evaluating while T
(or exp1 exp2 ... )keep evaluating while nil
(not exp1)reverse truth value
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)))
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
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)))
Let*
Same syntax but assignments made sequentially Example
(defun fielding (put-outs assists errors)(let* ((numerator (+ put-outs assists))
(denom (+ numerator errors)))
(/ numerator denom)))
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)
Extended Example
cards
Break
Lab CST 6th floor