Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Allegro CL Certification Program Lisp Programming Series Level I Session 1.1.1 Overview of Common Lisp."— Presentation transcript:

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

2 History of Lisp Born in 1958 John McCarthy 2nd oldest language still in active use Still on the leading edge

3 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

4 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

5 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

6 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

7 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

8 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

9 Famous Lisp Applications Yahoo Stores (http://store.yahoo.com)http://store.yahoo.com –Build a web site Orbitz (http://orbitz.com)http://orbitz.com –Search Airline Fares EMACS Mars Pathfinder planning Chandra and Hubble space telescope planning

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

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

12 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

13 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

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

15 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

16 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

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

18 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

19 Self-Evaluating Objects Numbers Strings And more...

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

21 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

22 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

23 Order of Evaluation Arguments are evaluated from left to right To see, try this: (defun foo (x y) nil) (foo (print “This”) (print “That”))

24 Try Out the Evaluator USER(2): (+ 2 3 4) 9 USER(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

25 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

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

27 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

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

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

30 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

31 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

32 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

33 Using Symbols (if (eq today 'Monday) …) (setq eye-color 'blue) (find 'meat ingredients)

34 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

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

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

37 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

38 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

39 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 USER(3):

40 Examples of Lists (1 2 3 4 5) ((1 2 3) 4 (5 6)) (1 'foo T NIL "frob")

41 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

42 Top-level loop great for performing experiments also great for debugging when developing code, use files

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

44 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

45 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


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

Similar presentations


Ads by Google