Artificial Intelligence and Lisp 12 The Programming Language



















![Lisp Function Definitions in. leo files ------------------------------- invassoc [: type entity] (defun invassoc (v Lisp Function Definitions in. leo files ------------------------------- invassoc [: type entity] (defun invassoc (v](https://slidetodoc.com/presentation_image/53b7bdd811998b88304fb3ecd830444b/image-20.jpg)
![Lisp Function Definitions in. leo files ------------------------------- invassoc [: type entity] [: latest-rearchived nil] Lisp Function Definitions in. leo files ------------------------------- invassoc [: type entity] [: latest-rearchived nil]](https://slidetodoc.com/presentation_image/53b7bdd811998b88304fb3ecd830444b/image-21.jpg)
![Lisp Function Definitions in. leo files --------------------------- invassoc [: type entity] (defun invassoc (v Lisp Function Definitions in. leo files --------------------------- invassoc [: type entity] (defun invassoc (v](https://slidetodoc.com/presentation_image/53b7bdd811998b88304fb3ecd830444b/image-22.jpg)


- Slides: 24

Artificial Intelligence and Lisp #12 The Programming Language LISP

Lab statistics 2009 -11 -20 Registration: 51 students Number of: Reg. Downloads Orphan downloads Lab completed Incomplete upload lab 2 a lab 2 b lab 3 a lab 3 b lab 4 a ----------------47 43 25 22 17 3 2 1 0 0 39 19 21 17 2 1 3 0 1 0

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)

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

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

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

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

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

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

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

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) (+ a 3) → 5 → 8 (list a 3) (list 'a a) → (5 3) → (a 5) (car '(a b c)) (cdr '(a b c)) (cons 'a '(d e f)) (cdr '(a)) (cons 'a nil) → → → a (b c) (a d e f) nil (a)

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

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)(reverse 2 x nil)) (defun reverse 2 (x y) (if (equal x nil) y (reverse 2 (cdr x) (cons (car x) y)) ))

Example of Lisp System Session, 4 (length “abcdef”) (concatenate 'string “abc” “def”) (subseq “abcdef” 2) (subseq “abcdef” 0 2) (put (get 'john → → 6 “abcdef” “ab” 'age 23) 'age (+ 1 (get 'john 'age))) 'age) (dolist (x '(a e i o u)) (put x 'vowel t)) → → 23 23 24 24 → 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

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 add 4 (adder 4)) → #<Closure. . > (funcall add 4 8) → 12

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) (funcall a 'set 20) (funcall a 'add 5) → 5 → 20 → 25

A few more constructs (caddr a) same as (car (cdr a))) All similar up to 4 car/cdr (progn (form 1)(form 2). . (formk)) Do operations with side-effects Value of (formk) is value of entire expression (cond (c 1 x 12. . x 1 k) (c 2 x 21. . ). . (cn xn 1 xn 2. . ) ) Same as (if c 1 (progn x 11 x 12. . x 1 k) (if c 2 (progn x 21 x 22. . ). . (if cn (progn xn 1 xn 2. . ) nil). . )) (setf (get a i) v) same as (put a i v)

Program Files (load ". . /Mia/Lab 5 a/cl/lab 5 a-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 Common. Lisp) are customarily used for files of this kind.

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
![Lisp Function Definitions in leo files invassoc type entity defun invassoc v Lisp Function Definitions in. leo files ------------------------------- invassoc [: type entity] (defun invassoc (v](https://slidetodoc.com/presentation_image/53b7bdd811998b88304fb3ecd830444b/image-20.jpg)
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 -------------------------------
![Lisp Function Definitions in leo files invassoc type entity latestrearchived nil Lisp Function Definitions in. leo files ------------------------------- invassoc [: type entity] [: latest-rearchived nil]](https://slidetodoc.com/presentation_image/53b7bdd811998b88304fb3ecd830444b/image-21.jpg)
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 -------------------------------
![Lisp Function Definitions in leo files invassoc type entity defun invassoc v Lisp Function Definitions in. leo files --------------------------- invassoc [: type entity] (defun invassoc (v](https://slidetodoc.com/presentation_image/53b7bdd811998b88304fb3ecd830444b/image-22.jpg)
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 )

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 fie)) -> 3 [Correct] writeloc-file-leo: . . /Mia/Lab 5 a/lab 5 a-report. leo writeloc-file-leos: . . /Mia/Lab 5 a/cl/lab 5 a-report. leos All test examples were done correctly - ready to upload

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)