1 / 18

Common Lisp Intro

Common Lisp Intro. Getting Started The Lisp interpreter Entering expressions for evaluation The debugger Basic Data Types Atoms, Symbols Building blocks of data with cons Functions Calling functions. Lisp Interpreter. Lisp on UH Unix and at home http://www.franz.com

chaka
Download Presentation

Common Lisp Intro

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Common Lisp Intro • Getting Started • The Lisp interpreter • Entering expressions for evaluation • The debugger • Basic Data Types • Atoms, Symbols • Building blocks of data with cons • Functions • Calling functions

  2. Lisp Interpreter • Lisp on UH Unix and at home • http://www.franz.com • http://www.lispworks.com • When Lisp is started • you are in the "read-evaluate-print loop" (REPL) • you see the Lisp prompt > • enter expressions for evaluation • Type in, or • copy/paste from another window , or • (load <filename> ) • reads in and evaluates the entire file

  3. Syntax Help • Look up syntax at • www.lisp.org/HyperSpec/FrontMatter/Master-Index.html • Contains • index and sections on various types of commands • fully hypertext • hints..

  4. Built-In Debugger • Errors in Lisp put you into the debugger • [<debug level>] is shown in the prompt • Learn common debugging commands • in ACL (Alegro Common Lisp) • :help prints list of commands • You must exit the debugger to continue • :reset, or • :pop

  5. The Debugger >(* 9 3 foo) ;CL prompts that an invalid expression was entered Error: Attempt to take the value of the unbound variable `FOO'. [condition type: UNBOUND-VARIABLE] Restart actions (select using :continue): 0: Try evaluating FOO again. 1: Set the symbol-value of FOO and use its value. 2: Use a value without setting FOO. 3: Return to Top Level (an "abort" restart) 4: Abort #<PROCESS Initial Lisp Listener> [1] >(11): :pop ; now we'll be back from debug level >(12):

  6. Statements and Examples • Comments - not executed • ;<semicolon> everything after ;on a line is a comment • Operators – categories depend on data type • numeric {+, /, …}, • logical {and, or, not}, predicates • string {append, subseq, …}, • list {cons, car, cdr, … }, … • Assignments – assign values to variables • Declarations – new variables, functions • Execution flow control – order of code execution • Input/Output

  7. Basic Types • Atoms • symbols (words), • numbers, • NIL – a special symbol meaning false or the empty list • Lists – objects/expressions enclosed in parentheses • ()the empty list, orNIL= false • (3 (4 5 6) a b c) • ((B (3))) • note the parentheses • extra parentheses are significant in Lisp (unlike in math) • (a b c) is a list of 3 items • ((a b c)) is a list of one item--that item being a list of 3 items • Strings – a sequence of characters within double quotes • "this is a string" • Objects and other structures

  8. Atoms • Numbers, symbols (words), and NILare atoms • Numbers are atoms • examples: 3, 8.9, 2/3, huge integers, etc. • Symbols are atoms • An object whose name is a string • Value - symbols are usually “variables” – they “bound” to other lisp objects • Symbol FOO might be bound to 4.2 • Function – function name bound to function definition • Other slots: property-list • NILis an atom • NILis both - an atom and a list

  9. T and NIL • T and NIL are distinguished symbols • are self-evaluating • cannot be bound to anything else • NIL • represents “false” in logical expressions (as a symbol) • represents the empty list (as a list) • can be written as () • T, or ANYTHING non-NIL • represents “true” in logical expressions. • (null <arg> ) • returns true if arg is NIL, false otherwise • i.e. returns T if arg is NIL, otherwise returns NIL • (listp <arg> ) • returns true if arg is a list (including NIL) • e.g., (listp NIL) => T

  10. Symbols • Common Lisp has both a value “slot” and a function value “slot” • Scheme has only one slot for value or function value • Packages provide separate name spaces (default is Common Lisp user) • Property lists can hold a lot of information (used in next Lisp assignments)

  11. Symbols – More than Variables!

  12. More Data Types & Built-In Functions • Lisp has a rich set of predefined data types • vectors • arrays • hashtables • strings • bitvectors • complex, floating point, integer, rational numbers • streams, pathnames • Predicates – functions that test the type of data • numberp, typep, atom, listp, integerp, …

  13. Variables • Valueshave types, not variables • Type declaration is not required • since a variable can hold objects of any type • but declaration improves efficiency • The built-in types form a hierarchy of subtypes and supertypes. • The type tis the supertype of all types • so everything is of type t

  14. Evaluation of Basic Types • Numbers • are “self-evaluating” • example: 5 => 5 • Symbols • Evaluate to their binding or value. • If COLOR has the value 3, then • COLOR => 3 • Alternatively, it could have the value of another symbol • COLOR => RED • Evaluation of an unbound symbol results in an error • Symbols have several facets in addition to a value • Strings • Are self-evaluating • Example: "this is a string" => "this is a string"

  15. Preventing Evaluation: Quote • (quote <arg>) • prevents the evaluation of its argument • its return value is <arg> • Single quote mark '<arg> • is equivalent to (quote <arg>) • Example: • 'foo is the same as (quote foo) • 'foo =>foo

  16. Functions • Functions are called using prefix notation • Parentheses () surround the call • 1st item is the function name or symbol • 2nd and remaining items are parameters (+ 1 3) => 4 (+ 1 3 5) => 9 + 1 4 3 1 + 3 9 5

  17. Functions Evaluation • A function call has the form • (<name> <arg1> <arg2> …) • All arguments <arg1> <arg2> … are evaluated (eagerly) • The function definition of <name> is obtained • The function is applied to the arguments • A value is returned

  18. Evaluation of Functions (cont.) • Evaluate all arguments • Get function binding for function name • (we will see special forms later) • Apply function to arguments • Examples: • (+ 5.3 8) =>13.3 • (+ (+ 4.5 (* 5.7 3)) (- 7 29) (/ 99 23))=>3.9043465 • in infix notation this amounts to (4.5+5.7*3)+(7-29)+(99/23) • if X is bound to 23, then (+ X 4) => 27

More Related