1 / 24

Artificial Intelligence and Lisp #12

The Programming Language LISP. Artificial Intelligence and Lisp #12. Registration: 51 students Number of: lab2a lab2b lab3a lab3b lab4a --------------------------------- Reg. Downloads 47 43 25 22 17

gboyce
Download Presentation

Artificial Intelligence and Lisp #12

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. The Programming Language LISP Artificial Intelligence and Lisp #12

  2. Registration: 51 students Number of: lab2a lab2b lab3a lab3b lab4a --------------------------------- Reg. Downloads 47 43 25 22 17 Orphan downloads 3 2 1 0 0 Lab completed 39 19 21 17 2 Incomplete upload 1 3 0 1 0 Lab statistics 2009-11-20

  3. Things to Learn about Lisp .... • .... or about any programming language: • Overall structure of the language and basic constructs in it • Competence to write full programs in that language • Characteristic and unusual aspects of the language in comparison with other programming languages • Walk-through in a segment of a typical program • What the language is used for, and what it is not used for • Available implementations, width of use, etc. • (green = taught here) (red = not taught here)

  4. LISP System Construction • 1. Define a simple datastructure and its textual representation, and make it as simple as possible • 2. Implement this datastructure as programs that convert from text to datastructure (input) and vice versa (output) • 3. Define how to express programs in this datastructure or (equivalently) in its textual representation • 4. Implement an interpreter for this (datastructure) representation of programs • 5. Done • Note: Leonardo uses the same idea, but its reportoire of commands is not a complete programming language

  5. S-expressions • The textual representation of data in Lisp is called S-expressions (symbolic expressions). Examples: • Elements: symbol ”this is a string” 14 3.14159 and some more specialized datatypes • Composite expressions: • (red green brown blue) • ((red rose poppy)(blue bluebell) (yellow dandelion)) • (red green blue . colors)

  6. Implementation of LISP data objects • Strings, integer and real numbers: Implementation dependent (details later) • Symbols: as a unique data object for each symbol, and technically as the memory address of that data object. The input routine is able to find the symbol object from the symbol's string. The symbol object is associated with several pieces of data (details later) • Lists: as a linked list of elements consisting of two pointers. Example: (red green brown blue) nil red green brown blue

  7. Operations on LISP data objects • Lists: as a linked list of elements consisting of two pointers. Example: (red green brown blue) • tl[(red green brown blue)] = (green brown blue) • hd[(red green brown blue)] = red nil red green brown blue

  8. Operations on LISP data objects • Lists: as a linked list of elements consisting of two pointers. Example: (red green brown blue) • cons[yellow, (red green brown blue)] = • (yellow red green brown blue) yellow nil red green brown blue

  9. Elementary Operations in Lisp • Hd (also called car), tl (also called cdr) • Cons • Conditional expression (if, cond) • Test for equality • Test what type of object the argument is: symbol, string, number, list • Defining and using functions, even recursively • Usual operations on numbers and strings, e.g. addition, concatenation, obtaining substrings • Basic input and output, to screen and files • .... This is sufficient for defining a functioning system, everything else can be defined in the language itself

  10. Summary of functions and operations + also -, *, / if Equal also = but only for num args setq list car same as hd cdr same as tl cons defun type-of length concatenate 'string subseq put get dolist

  11. Example of Lisp System Session, 1 (+ 4 5) → 9 (if (equal 3 4) 6 8) → 8 (equal 3 4) → nil (equal 4 4) → t (setq a 5) → 5 (+ a 3) → 8 (list a 3) → (5 3) (list 'a 3) → (a 3) (list 'a a) → (a 5) (car '(a b c)) → a (cdr '(a b c)) → (b c) (cons 'a '(d e f)) → (a d e f) (cdr '(a)) → nil (cons 'a nil) → (a)

  12. Example of Lisp System Session, 2 (defun foo (x y) (+ x (* y 3))) → foo (foo 2 3) → 11 (defun abs (x) (if (> x 0) x (- x))) → abs (abs 4) → 4 (abs -3.14) → 3.14 (defun fac (n) (if (equal n 0) 1 (* n (fac (- n 1)))) ) → fac (fac 4) → 24 (type-of (fac 4)) → fixnum (type-of (fac 6000)) → bignum

  13. Example of Lisp System Session, 3 (defun append (x y) (if (equal x nil) y (cons (car x)(append (cdr x) y)) )) (defun reverse (x) (if (equal x nil) nil (append (reverse (cdr x)) (list (car x)) ))) (defun reverse (x)(reverse2 x nil)) (defun reverse2 (x y) (if (equal x nil) y (reverse2 (cdr x) (cons (car x) y)) ))

  14. Example of Lisp System Session, 4 (length “abcdef”) → 6 (concatenate 'string “abc” “def”) → “abcdef” (subseq “abcdef” 2) → “cdef” (subseq “abcdef” 0 2) → “ab” (put 'john 'age 23) → 23 (get 'john 'age) → 23 (put 'john 'age (+ 1 (get 'john 'age))) → 24 (get 'john 'age) → 24 (dolist (x '(a e i o u)) (put x 'vowel t)) → nil (setq counter 0) (setq alphabet '(a b c d e f g h i j k l m etc)) (dolist (let alphabet) (setq counter (+ 1 counter)) (put let 'number-in-alphabet counter) ) (get 'd 'number-in-alphabet) → 4

  15. Example of Lisp System Session, 5 (funcall (function cons) 'a '(b c)) → (a b c) (defun adder (x) (function (lambda (y) (+ x y))) ) → adder (funcall (adder 4) 8) → 12 (setq add4 (adder 4)) → #<Closure..> (funcall add4 8) → 12

  16. Example of Lisp System Session, 6 (defun miniagent (init) (function (lambda (msg arg) (if (equal msg 'set) (setq init arg) (+ init arg) )))) (setq a (miniagent 0)) → #<closure ...> (funcall a 'add 5) → 5 (funcall a 'set 20) → 20 (funcall a 'add 5) → 25

  17. A few more constructs (caddr a) same as (car (cdr (cdr a))) All similar up to 4 car/cdr (progn (form1)(form2) .... (formk)) Do operations with side-effects Value of (formk) is value of entire expression (cond (c1 x11 x12 .... x1k) (c2 x21 x21 .... ) .... (cn xn1 xn2 .... ) ) Same as (if c1 (progn x11 x12 .... x1k) (if c2 (progn x21 x22 ....) .... (if cn (progn xn1 xn2 ....) nil) ..)) (setf (get a i) v) same as (put a i v)

  18. Program Files (load "../../../Mia/Lab5a/cl/lab5a-defs.cl") This function reads the specified file, which should contain a sequence of S-expressions, and evaluates them one after the other. Typically the file contains expressions headed by the operators defun and setf. A semicolon marks the rest of the line as a comment, except inside strings (and some other, odd situations) The extensions .lsp and .cl (for CommonLisp) are customarily used for files of this kind.

  19. Some Important Software Techniques in Lisp • Programs for administrating programs (analyzing them, operating on them, etc) • Use of software platforms (= engines) that support scripting languages (often = special-purpose languages) • Details on the following slides

  20. Lisp Function Definitions in .leo files ------------------------------------------------------------- -- invassoc [: type entity] (defun invassoc (v al) (cond ((null al) nil) ((member v (cdar al)) (caar al)) (t (invassoc v (cdr al))) )) @Comment This function obtains the inverted use of an associationlist for a specific example, e.g. (invassoc 'd '((red a b c)(green d e f)(blu g h I))) evaluates to the symbol green -------------------------------------------------------------

  21. Lisp Function Definitions in .leo files ------------------------------------------------------------- -- invassoc [: type entity] [: latest-rearchived nil] added by the system when the Entityfile is written – ignore it (defun invassoc (v al) (cond ((null al) nil) ((member v (cdar al)) (caar al)) (t (invassoc v (cdr al))) )) @Comment This function obtains the inverted use of an associationlist for a specific example, e.g. (invassoc 'd '((red a b c)(green d e f)(blu g h I))) evaluates to the symbol green -------------------------------------------------------------

  22. Lisp Function Definitions in .leo files ---------------------------------------------------- -- invassoc [: type entity] (defun invassoc (v al) (cond ((null al) nil) ((member v (cdar al)) (caar al)) (t (invassoc v (cdr al))) )) @Testexamples ((dandelion ((red rose poppy)(blue forgetmenot bluebell) (yellow sunflower dandelion) )) yellow) ((eyebright ((red rose poppy)(blue forgetmenot bluebell) (yellow sunflower dandelion) )) nil ) ((Groucho ((horse Hope Frivolous Dandy)(cat Pussy Minnie) (gorilla Groucho Gredelina) )) gorilla )

  23. Application of Test Examples 004) check-lab ------------------------------------------- Test results for: invassoc (dandelion ((red rose poppy) (blue forgetmenot bluebell) (yellow sunflower dandelion))) -> yellow [Correct] (eyebright ((red rose poppy) (blue forgetmenot bluebell) (yellow sunflower dandelion))) -> nil [Correct] (Groucho ((horse Hope Frivolous Dandy) (cat Pussy Minnie) (gorilla Groucho Gredelina))) -> gorilla [Correct] ------------------------------------------- Test results for: occurrences (foo (fie foo fum foo foo fie)) -> 3 [Correct] writeloc-file-leo: ../../../Mia/Lab5a/lab5a-report.leo writeloc-file-leos: ../../../Mia/Lab5a/cl/lab5a-report.leos All test examples were done correctly - ready to upload

  24. Self-administration of software • One advantage of the program/data equivalence in Lisp is that it makes it very easy to implement services that administrate your own programs (and data) • Self-administration services can be defined so as to operate on conventional Lisp files (.cl or .lsp) • The use of the Leonardo framework makes it possible to introduce and use program-related information in a more systematic way • The check-lab facility and the handling of test examples illustrates this (see software files)

More Related