24
Artificial Intelligence and Lisp #12 The Programming Language LISP

Artificial Intelligence and Lisp #12 The Programming Language LISP

  • View
    285

  • Download
    6

Embed Size (px)

Citation preview

Page 1: Artificial Intelligence and Lisp #12 The Programming Language LISP

Artificial Intelligence and Lisp #12

The Programming Language LISP

Page 2: Artificial Intelligence and Lisp #12 The Programming Language LISP

Lab statistics 2009-11-20

Registration: 51 students

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

Page 3: Artificial Intelligence and Lisp #12 The Programming Language LISP

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)

Page 4: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 5: Artificial Intelligence and Lisp #12 The Programming Language LISP

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)

Page 6: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

bluebrowngreenred

Page 7: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

bluebrowngreenred

Page 8: Artificial Intelligence and Lisp #12 The Programming Language LISP

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)

nil

bluebrowngreenred

yellow

Page 9: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 10: Artificial Intelligence and Lisp #12 The Programming Language LISP

Summary of functions and operations

+ also -, *, /ifEqual also = but only for num argssetqlistcar same as hdcdr same as tlconsdefuntype-oflengthconcatenate 'stringsubseqputgetdolist

Page 11: Artificial Intelligence and Lisp #12 The Programming Language LISP

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)

Page 12: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 13: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 14: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 15: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 16: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 17: Artificial Intelligence and Lisp #12 The Programming Language LISP

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)

Page 18: Artificial Intelligence and Lisp #12 The Programming Language LISP

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.

Page 19: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

Page 20: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

-------------------------------------------------------------

Page 21: Artificial Intelligence and Lisp #12 The Programming Language LISP

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

-------------------------------------------------------------

Page 22: Artificial Intelligence and Lisp #12 The Programming Language LISP

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 )

Page 23: Artificial Intelligence and Lisp #12 The Programming Language LISP

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.leowriteloc-file-leos: ../../../Mia/Lab5a/cl/lab5a-report.leosAll test examples were done correctly - ready to upload

Page 24: Artificial Intelligence and Lisp #12 The Programming Language LISP

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)