Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common...
-
Upload
suzanna-cecilia-berry -
Category
Documents
-
view
226 -
download
0
Transcript of Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common...
Allegro CL Certification Program
Lisp Programming Series Level I Session 1.1.1
Overview of Common Lisp
History of Lisp
• Born in 1958
• John McCarthy
• 2nd oldest language still in active use
• Still on the leading edge
History of Lisp, cont’d
• Earliest widespread dialect called Lisp 1.5
• Bloomed into serious development environments but fragmented– Interlisp, MacLisp
• Standardization effort led to Common Lisp, CLtL1, CLtL2, CLOS
• Common Lisp has itself evolved, most recently into an ANSI standard
The original RAD Environment
• Rapid Application Development
• Write a prototype faster than the spec
• Iterate the development for performance
• Refine prototype into production quality code
Thrives on Complex Problems
• Lisp is in a league of its own when problems are complex
• You can’t spec something if you have never solved a problem like this - you have to experiment and prototype
• Lisp excels at prototyping
• The Lisp Environment is well suited to supporting VERY large programs
Dynamic Programming Language
• ‘Dynamic’ means you can add or redefine functions while the program is running
• Change a function, compile it, load it, and test it without restarting the application
• Very fast edit-debug cycle• Frequent development strategy: use standin functions
for something complicated that will be needed eventually– standin goes into debugger– standin returns dummy values
Myths About Lisp
• Lisp is interpreted– Normally, you compile your code
• Lisp is slow– If compiled, its fast as the underlying machine
• Lisp is big– It is big for a Hello, World application– It is not big for real applications– My MS Office directory is 1.6 GB
More Lisp Myths
• Weird syntax– (+ a b) instead of a + b takes some getting used
to– (+ a (/ b c)) less ambiguous than a + b / c– Parsing lisp programs is trivially easy due to the
prefix notation
• Garbage collector is slow– If you notice it at all, something is probably wrong
Famous Lisp Applications
• Yahoo Stores (http://store.yahoo.com)– Build a web site
• Orbitz (http://orbitz.com)– Search Airline Fares
• EMACS• Mars Pathfinder planning• Chandra and Hubble space telescope planning
Lisp Syntax
• Prefix notation– Function name followed by zero or more args
• Delimited by parentheses
• (+ 2 3 4)
• (* (- 7 1) (- 4 2) 2)
• (print “Hello, world”)
Examples of Lisp Syntax
• (solve-polynomial 1 5 7 9)
• (if (eq weather 'cold) (turn-on-heat) (turn-on-ac))
• (dotimes (i 5) (print i))
Function Definitions
• Use defun• Specify function name, list of argument names,
then body of function
• (defun square (x) (* x x))
• No requirement to declare the type of x, as you do in C, C++ or Java
Lisp Syntax Simplicity
• That’s really all there is to know about the syntax of the language
• It is as clean and simple as you can get
• No ambiguities, no special cases
• This simplicity can make it powerfully flexible
Lisp is Rich• ANSI Common Lisp
– 978 symbols some of which name operators, variables, and constants
• Lots of things you may never use– ODDP: true if an integer is an odd number
• Lots of things to you might have to write for yourself in another language– Sorting and searching algorithms– Hash tables– Adjustable length arrays
• Saves you time in the long run (in the short run you've got a lot to learn)
Everything Is An Object
• The value of every variable is an object– A variable is not just a way to reference storage
• Everything has a type that can be discovered
• Everything (almost) can move (due to GC)
• Everything has properties about which one can ask questions
Program Data is Freed Automatically
• In Lisp there is no free()
• The job of the garbage collector is to notice objects that are not referenced anywhere
• The garbage collector runs periodically, not continuously
• When triggered, the GC stops lisp long enough to copy “live” objects into a new area, then lisp continues
Evaluation
• When a lisp program is “run”, the call to run the program is evaluated. That is, it is processed by a Common Lisp function called eval.
Evaluation cont’d
• There are three kinds of objects which can be evaluated.– Symbol– a list of which the first element is the name of
an operator (a compound form)– a self-evaluating object
Self-Evaluating Objects
• Numbers
• Strings
• And more...
Expression Evaluation
If the expression is a list of which the first element names a function, then:
• Each of the elements in the list, after the function name is evaluated.
• The result of each such evaluation is passed to the function as an argument.
Expression Evaluation cont’d
• (+ 2 3 4)• “+” names a function
• (+ 2 3 4) is a function call with args 2, 3 and 4
• Value of the arguments are passed to the function
• Function returns a value
• Evaluating an expression always returns a value
Argument Evaluation
• If any of the args are themselves expressions they are evaluated in the same manner.
• The sub-expression is evaluated and its result passed as an arg
• (* (- 7 1) (- 4 2) 2)– returns 24
Order of Evaluation
• Arguments are evaluated from left to right• To see, try this:
(defun foo (x y)
nil)
(foo (print “This”)
(print “That”))
Try Out the Evaluator
USER(2): (+ 2 3 4)9USER(3): (* (- 7 1) (- 4 2) 2)24
• Type expression to the Lisp prompt and hit Enter• Lisp reads what you typed, evaluates it, and
prints the return value
Lisp Data types
• Usual Data Types in Other Languages– Numbers: 123, 1.45, -10e2, 3/5, #C(2 3)– Strings: "This is a fine day"
• Fundamental Lisp Data types– Symbols: HELLO, FOO, START– Lists: (a (b c) d)– Characters: #\A, #\z, #\space
Quote
When anything immediately preceded by a ‘ is evaluated the value returned is the object which followed the ‘ .
• a is not the same as ‘a
• (a) is not the same as ‘(a)
• ‘a is equivalent to (quote a)
• ‘(a b c) is equivalent to (quote (a b c))
What are Symbols?
• A symbol is an object that has a name and possibly a value.
• Symbols have other attributes, including:– Possibly an associated function definition– Property list
What are Symbols, cont’d
• The special operator setq and the macro setf can be used to change the value of a variable named by a symbol:– (setq color 'red) – (setq mnop z)– (setf qrst 'z)– Quote (') says don’t use the value of the symbol red, use
the symbol itself.
Variable Assignment Using setq and setf
• The second element of the list is NOT evaluated• The third element of the list is evaluated • (setq x 35.0)• The “variable” being “set” is named by the
symbol x (setf y 37.0)
(setf z (+ 2 3))
t and nil
• There are distinguished symbols t and nil
• Variables named by the symbols t and nil ALWAYS evaluate to themselves– t evaluates to t – nil evaluates to nil
Program Data is Typed, Variables are NOT typed
• (setq x 35) ; value of x is integer• (setq x 35.0) ; value of x is float
• In most other languages, x could hold an integer or a float but not both
• Variables have no inherent type• Values (memory locations) are objects that carry
type information
Data Equality
• internally Lisp refers to most objects via pointers
• fundamental equality operation is EQ– only true if the two arguments point to the same
object– test is very efficient
• EQL is default
Using Symbols
• (if (eq today 'Monday) …)
• (setq eye-color 'blue)
• (find 'meat ingredients)
ANSI Lisp is Case-Insensitive
• These lines are all equivalent:– (setq color ‘red) ; downcase– (Setq Color ‘Red) ; capitalized– (setq color ‘RED) ; uppercase
• By convention, most lisp code is lower case• Allegro Common Lisp lets you create a case-
sensitive lisp
Lists
• Lists are fundamental to LISP – LISP = LISt Processing
• Lists are zero or more elements enclosed by parentheses
• You have to quote them or Lisp will assume you are calling a function
• '(red green blue)
Lists, cont’d
• Lists have a printed representation of open parenthesis, followed by the list elements, followed by close parenthesis.
• Sounds familiar, no? – Lisp source code is just lists of lisp expressions.
• Printed representation can be read in too.
Overview of Lists - Creation
• Typing a list to the Lisp Listener
• '(this is a list) --> (THIS IS A LIST)
• Creating a list with code:
• (list 'so 'is 'this) --> (SO IS THIS)
These are NOT equivalent
Input
• (read *standard-input*)– Reads one Lisp object from *standard-input*– Examples of “one Lisp object”:
• String: “hello, world”
• Symbol: RED
• List: (one two three)
• Number: 3.1415
– Second argument is optional
Output
• (print 'hello *standard-output*) – Prints HELLO to *standard-out*– Returns the symbol HELLO as its value– Stream argument is optional
USER(2): (print 'hello)
HELLO HELLO
USER(3):
Examples of Lists
• (1 2 3 4 5)• ((1 2 3) 4 (5 6))• (1 'foo T NIL "frob")
Lisp Programs
• A Lisp program consists of one or more lists
• It is very easy for Lisp programs to generate and execute Lisp code
• This is NOT true of most other languages
Top-level loop
• great for performing experiments
• also great for debugging
• when developing code, use files
Edit/compile/debug
• Use Emacs on UNIX or IDE on Windows to create source code in files.
• Compile the code into object files (*.fasl)
• Run, test, and debug the program
• Edit an individual definition
• Compile an individual definition
• Go to “Run, Test and Debug”
Edit/compile/debug cont’d
• Profile speed to find bottlenecks
• Profile memory allocation
• Optimize code with compiler declarations
• Trace function calls and set breakpoints to characterize control flow
• Find and edit the callers of a function
Summary
• Lisp is a very rich language– there are very many predefined useful functions– can’t remember all details of all, but don’t need
to
• Extremely easy to add your own functions