Artificial Intelligence and Lisp #12 The Programming Language LISP.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
Lisp II. How EQUAL could be defined (defun equal (x y) ; this is how equal could be defined (cond ((numberp x) (= x y)) ((atom x) (eq x y)) ((atom y)
1 Programming Languages and Paradigms Lisp Programming.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Artificial Intelligence and Lisp #3 Characterization of Actions [Chapter 5] Representation Language [Chapter 2] Lab Assignment 2.
1 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester, 2010
Artificial Intelligence and Lisp Lecture 12 Finish "Programming in Lisp, II" + Lab 5 a-b + Review and Synthesis of the Course LiU Course TDDC65 Autumn.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
General pattern for selecting some elements of a list This negatives example illustrates a general pattern: If you want a function which selects some elements.
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
F UNCTIONAL P ROGRAMMING 05 Functions. F UNCTIONS - G LOBAL F UNCTIONS fboundp Tells whether there is a function with a given symbol as its name > (fboundp.
Functional Programming
1 Lists in Lisp and Scheme. 2 Lists are Lisp’s fundamental data structures. Lists are Lisp’s fundamental data structures. However, it is not the only.
Common Lisp Macros Read for Input Macros Macro lifetime Macro syntax
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lisp Laboratory gcLisp (Golden Common Lisp). Lect. ratchadaporn kanawong2 The history of Lisp In summer 1956, Allen Newell, J.C. Shaw, and Herbert Simon.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming in Scheme and Lisp.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Introduction to LISP. Lisp Extensible: It lets you define new operators yourself Lisp programs are expressed as lisp data structures –You can write programs.
UMBC CMSC Common Lisp II. UMBC CMSC Input and Output Print is the most primitive output function > (print (list 'foo 'bar)) (FOO BAR) The.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Comparative Programming Languages Functional programming with Lisp/Scheme.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
LISP LISt Processing. History & Overview b b One of the oldest high level programming languages. b b First developed in 1958 by John McCarthy. b b Later.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Artificial Intelligence and Lisp Lecture 11 Programming in Lisp, II LiU Course TDDC65 Autumn Semester,
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
Functional Programming Languages
Artificial Intelligence and Lisp Lecture 3 LiU Course TDDC65 Autumn Semester,
Section 15.4, 15.6 plus other materials
Artificial Intelligence and Lisp #3
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lists in Lisp and Scheme
LISP LISt Processing.
LISP A brief overview.
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Data abstraction, revisited
CS 36 – Chapter 11 Functional programming Features Practice
Modern Programming Languages Lecture 20 Fakhar Lodhi
The Metacircular Evaluator (Continued)
CSE S. Tanimoto Explicit Function Application
Lisp: Using Functions as Data
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
LISP LISt Processing.
Abstraction and Repetition
Lisp: Using Functions as Data
LISP LISt Processing.
Modern Programming Languages Lecture 18 Fakhar Lodhi
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Lisp.
More Scheme CS 331.
Presentation transcript:

Artificial Intelligence and Lisp #12 The Programming Language LISP

Lab statistics Registration: 51 students Number of: lab2a lab2b lab3a lab3b lab4a Reg. Downloads Orphan downloads Lab completed Incomplete upload

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 S- expressions (symbolic expressions). Examples: Elements: symbol ”this is a string” and some more specialized datatypes Composite expressions: (red green brown blue) ((red rose poppy)(blue bluebell) (yellow dandelion)) (red green 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 blue browngreenred

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 blue browngreenred

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 blue browngreenred yellow

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

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)

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

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

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

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

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 x x1k) (c2 x21 x ).... (cn xn1 xn2.... ) ) Same as (if c1 (progn x11 x x1k) (if c2 (progn x21 x22....).... (if cn (progn xn1 xn2....) nil)..)) (setf (get a i) v) same as (put a i v)

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.

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 al) (cond ((null al) nil) ((member v (cdar al)) (caar al)) (t (invassoc v (cdr al))) 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] [: 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))) 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 al) (cond ((null al) nil) ((member v (cdar al)) (caar al)) (t (invassoc v (cdr al))) ((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 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

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)