Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common...

45
Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp

Transcript of Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common...

Page 1: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

Allegro CL Certification Program

Lisp Programming Series Level I Session 1.1.1

Overview of Common Lisp

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

Page 3: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 4: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 5: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 6: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 7: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 8: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 9: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 10: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 11: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 12: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 13: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 14: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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)

Page 15: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 16: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 17: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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.

Page 18: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 19: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

Self-Evaluating Objects

• Numbers

• Strings

• And more...

Page 20: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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.

Page 21: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 22: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 23: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

Order of Evaluation

• Arguments are evaluated from left to right• To see, try this:

(defun foo (x y)

nil)

(foo (print “This”)

(print “That”))

Page 24: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 25: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 26: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 27: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 28: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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.

Page 29: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 30: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 31: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 32: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 33: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

Using Symbols

• (if (eq today 'Monday) …)

• (setq eye-color 'blue)

• (find 'meat ingredients)

Page 34: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 35: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common 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)

Page 36: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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.

Page 37: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 38: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 39: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 40: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

Examples of Lists

• (1 2 3 4 5)• ((1 2 3) 4 (5 6))• (1 'foo T NIL "frob")

Page 41: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 42: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

Top-level loop

• great for performing experiments

• also great for debugging

• when developing code, use files

Page 43: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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”

Page 44: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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

Page 45: Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp.

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