1 / 40

Introduction

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 rburke@cs.depaul.edu Programming in Lisp for about 20 years would program in nothing else if given the choice.

braima
Download Presentation

Introduction

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. Introduction CSC 358/458 3/27/2006

  2. Outline • Introductions • Course goals • Course organization • History of Lisp • Lisp syntax • Examples • Lisp IDE

  3. Me • Robin Burke • CST 453 • rburke@cs.depaul.edu • Programming in Lisp for about 20 years • would program in nothing else if given the choice

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

  5. Lisp environment • Allegro Common Lisp • free download www.franz.com • labs • terminal server • There are other implementations including open-source ones

  6. Symbolic Programming • Programming as manipulation of symbols

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

  8. Math with Symbols

  9. Symbolic Programming • Symbolic representation of a problem • Formal operations to manipulate the representations • Closely related to AI

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

  11. Topics • Introduction • Lists and Trees • Functions • Strings and I/O • Midterm • Control structures • AI application: pattern matching • Macros • AI application: search

  12. Lisp origins • 2nd Oldest Programming Language • John McCarthy, MIT 1960s • Originally intended as an internal language for working with FORTRAN programs • developed a life of its own • LISt Processing

  13. Lisp History • Early implementations • very slow • purely interpreted • widely varying semantics • different function libraries • 80s • AI boom • need for portable Lisp • CommonLisp initiative

  14. Lisp Today • Many Lisp ideas become mainstream • prototyping • dynamic memory management • functions as first-class objects • Modern Lisp • very fast • multi-paradigm programming • extensible

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

  16. Syntax • Basic elements of the language • Relationships between them

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

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

  19. Lisp syntax 3 • Every expression is either • a list • (a b c) • an atom • 5 • Atoms are evaluated • some self-evaluate • symbols look for bindings

  20. Lisp syntax 4 • Lists • the first element is evaluated as a function • each other element is evaluated • then the function is called with the other elements as arguments • a value is returned

  21. Exception • If first element • is the name of a special form or macro • then all arguments may not be evaluated • Example • (if (and (< health 5) (< enemy-distance 10)) (self-destruct) (fire-weapon))

  22. Evaluation • a • pi • “a” • #\a • (cos pi) • ‘(cos pi)

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

  24. Quote • Prevents evaluation • crucial for symbols • Obviously a special form • (quote a) => a • Abbreviation • ' • 'a => a

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

  26. Lists • Basic Lisp data type • (a b c) • Front • a • "car" • Rest • (b c) • "cdr" • nil = empty list

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

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

  29. 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 setq • first argument can be a function call • (setf (car l) 0)

  30. Defining functions • (defun name (args) body) • Examples • (defun square (x) (* x x)) • (defun sum-sq (x y) (+ (* x x) (* y y))) • Recursive function • factorial

  31. Truth values • T/nil • actually any non-nil value is assumed to be T

  32. Conditions • Syntax • (if exp1 exp2 [exp3]) • If exp1 evaluates to T • evaluate exp2 and return the value • otherwise evaluate exp3 and return it • if no exp3, return nil

  33. And / Or / Not • (and exp1 exp2 ... ) • keep evaluating while T • (or exp1 exp2 ... ) • keep evaluating while nil • (not exp1) • reverse truth value

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

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

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

  37. Let* • Same syntax • but assignments made sequentially • Example (defun fielding (put-outs assists errors) (let* ((numerator (+ put-outs assists)) (denom (+ numerator errors))) (/ numerator denom)))

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

  39. Extended Example • cards

  40. Break • Lab • CST 6th floor

More Related