Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.

Slides:



Advertisements
Similar presentations
09 Examples Functional Programming. Tower of Hanoi AB C.
Advertisements

ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
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)
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
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)
Helper functions: when extra arguments are needed Consider this problem: we want a function index_items that takes a list L and gives a number to each.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
1 LISP III. 2 Functional programming l Definition From the "comp.lang.functional FAQ" Functional programming is a style of programming that emphasizes.
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.
CSCI Programming in Lisp; Instructor: Alok Mehta; 3_structs.ppt1 CSCI 2210: Programming in Lisp Programming Techniques Data Structures More Built-in.
Returning values from functions You can return a value from a function by using the built- in function : ( return-from Function_name value) For example:
>(setf oldlist ) Constructing a list We know how to make a list in lisp; we simply write it: ‘4321( ) What if we want a new list with that list as a part?
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
CMSC 471 LISP. Why Lisp? Because it’s the most widely used AI programming language Because it’s good for writing production software (Graham article)
CSE S. Tanimoto Explicit Function Application 1 Explicit Application of Functions, Functional Arguments and Explicit Evaluation Implicit and explicit.
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
ANSI Common Lisp 4. Specialized Data Structures 7 June 2003.
How to define functions? (DEFUN function-name parameter-list commands ) Example: (DEFUN myFunction (x y) (/ x y) ) To call this function, you would then.
Recursion. Definitions I A recursive definition is a definition in which the thing being defined occurs as part of its own definition Example: A list.
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.
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.
TUTORIAL 2 CSCI3230 ( First Term) By Paco WONG 1.
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.
Advanced Functions In CL, functions are often supplied as parameters to other functions –This gives us tremendous flexibility in writing functions whose.
Functional Programming 02 Lists
Lecture 2-2CS250: Intro to AI/Lisp Implementing Search Lecture 2-2 January 14 th /19 th, 1999 CS250.
PRACTICAL COMMON LISP Peter Seibel 1.
Mitthögskolan 10/8/ Common Lisp LISTS. Mitthögskolan 10/8/2015 2Lists n Lists are one of the fundamental data structures in Lisp. n However, it.
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.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Lecture 2-1CS250: Intro to AI/Lisp Intelligent Agents Lecture 3-2 October 14 th, 1999 CS250.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
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.
Lecture 4-2CS250: Intro to AI/Lisp Better Living through Search II Lecture 4-2 October 21 st, 1999 CS250.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Variable Declarations Global and special variables – (defvar …) – (defparameter …) – (defconstant …) – (setq var2 (list 4 5)) – (setf …) Local variables.
Lists CSC 358/ Outline Lab #1 / Homework #1 Lists A question List Internals of Lists List operations Extended Example.
Basic Introduction to Lisp
Search as a problem solving technique. Consider an AI program that is capable of formulating a desired goal based on the analysis of the current world.
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
CS 550 Programming Languages Jeremy Johnson
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Lists in Lisp and Scheme
CS 270 Math Foundations of CS Jeremy Johnson
LISP A brief overview.
J.E. Spragg Mitthögskolan 1997
Modern Programming Languages Lecture 20 Fakhar Lodhi
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: Using Functions as Data
Common Lisp II.
Programming Languages
Lisp.
Lists in Lisp and Scheme
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

Lecture 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250

Lecture 6-2CS250: Intro to AI/Lisp “Get Your Red-Hot Lists Here!” Conses are pairs of pointers –First pointer is the car –Rest is the cdr Lists are conses in which: –First pointer is the first element –Second pointer is the rest of the list –No intermediate pointers makes last expensive USER(104): (last (list 'a 'b 'c)) (C)

Lecture 6-2CS250: Intro to AI/Lisp Box & Pointer Represent a cons graphically (list ‘a (list ‘b ‘c) ‘d)

Lecture 6-2CS250: Intro to AI/Lisp Some Things are More Equal than Others Lisp has multiple definitions of equality Decreasing order of strictness –eq, eql, equal

Lecture 6-2CS250: Intro to AI/Lisp eq True if its arguments are the same, identical object; otherwise, returns false (eq 'a 'b) => false (eq 'a 'a) => true (eq 3 3) => true OR => false (eq 3 3.0) => false

Lecture 6-2CS250: Intro to AI/Lisp eql True of two objects, x and y, in the folowing cases: 1. If x and y are eq. 2. If x and y are both numbers of the same type and the same value. 3. If they are both characters that represent the same character. (eql 'a 'b) => false (eql 'a 'a) => true (eql 3 3) => true (eql 3 3.0) => false (eql ) => true (eql #c(3 -4) #c(3 -4)) => true (eql #c(3 -4.0) #c(3 -4)) => false

Lecture 6-2CS250: Intro to AI/Lisp equal Generally, returns true if two objects print the same > (setf x (cons ‘a nil)) (A) > (eql x x) T > (equal x (cons ‘a nil)) T

Lecture 6-2CS250: Intro to AI/Lisp Mapping over lists Need to do something to every element in a list? Try a mapping function: –mapcar for using the car of successive cdr’s –maplist for successive cdr’s themselves

Lecture 6-2CS250: Intro to AI/Lisp mapcar in Action USER(115): (mapcar #'list '(a b c) '( )) USER(116): (mapcar #'list '(a b c) '(1 2)) ((A 1) (B 2) (C 3)) ((A 1) (B 2))

Lecture 6-2CS250: Intro to AI/Lisp Creating an N-Queens Problem (defun nqueens-initial-state (n &optional (explicit? nil) (complete? nil)) (let ((s (make-CSP-state :unassigned (mapcar #'(lambda (var) (make-CSP-var :name var :domain (iota n))) (iota n)) :assigned nil :constraint-fn (if explicit? (let ((constraints (nqueens-constraints n))) #'(lambda (var1 val1 var2 val2) (CSP-explicit-check var1 val1 var2 val2 constraints))) #'nqueens-constraint-fn)))) (if complete? (CSP-random-completion s) s)))

Lecture 6-2CS250: Intro to AI/Lisp Unassigned Variables (mapcar #'(lambda (var) (make-CSP-var:name var :domain (iota n))) (iota n)) USER(105): (iota 8) ( )

Lecture 6-2CS250: Intro to AI/Lisp Unassigned Variables II ((0 ( ) NIL NIL) (1 ( ) NIL NIL) (2 ( ) NIL NIL) (3 ( ) NIL NIL) (4 ( ) NIL NIL) (5 ( ) NIL NIL) (6 ( ) NIL NIL) (7 ( ) NIL NIL))

Lecture 6-2CS250: Intro to AI/Lisp Recursion Again Recursive function = Base case + Recursive step –Base case will be a conditional test, plus a call that returns Example: General-Search (defun general-search-helper (problem nodes queuing-fn) (let ((node (first nodes))) (if (null node) nil :

Lecture 6-2CS250: Intro to AI/Lisp Recursive General Search (if (goal-test problem (node-state node)) node (general-search-helper problem (funcall queuing-fn (rest nodes) (expand node problem)) queuing-fn)...) If we’ve got a node, what do we do next? What if it’s not the goal?

Lecture 6-2CS250: Intro to AI/Lisp Put it Together (defun general-search-helper (problem nodes queuing-fn) (let ((node (first nodes))) (if (null node) nil (if (goal-test problem (node-state node)) node (general-search-helper problem (funcall queuing-fn (rest nodes) (expand node problem)) queuing-fn)))))

Lecture 6-2CS250: Intro to AI/Lisp Getting it Started... (let ((nodes (make-initial-queue problem queuing-fn)) node) (defun make-initial-queue (problem queuing-fn) (let ((q (make-empty-queue))) (funcall queuing-fn q (list (create-start-node problem))) q)) From simple.lisp : General-Search function How does Make-Initial-Queue work?

Lecture 6-2CS250: Intro to AI/Lisp Top-level Function (defun general-search-recursive (problem queueing-fn) "Recursive version of general search" (general-search-helper problem (list (create-start-node problem)) queueing-fn))

Lecture 6-2CS250: Intro to AI/Lisp BFS with a List (defun breadth-first-search (problem) (general-search-recursive problem #'append)) What’s the rule for node exploration in BFS? How are new nodes added?

Lecture 6-2CS250: Intro to AI/Lisp Sets Sets let you treat lists as sets –Membership –Union, intersection –Set difference

Lecture 6-2CS250: Intro to AI/Lisp Sequences Sequences include more than just lists –Ordered series –Lists and vectors Many functions operate on sequences, not just lists: –length, sort, subseq, reverse, every, some, elt

Lecture 6-2CS250: Intro to AI/Lisp Structures Create records in Lisp Define structures with the defstruct macro: (defstruct point x y)

Lecture 6-2CS250: Intro to AI/Lisp “Big Sale on Constructors & Accessors!” Creating a structure creates: –Constructor ( make-point ) Arguments are passed by keyword –Copy constructor ( copy-point ) –Slot accessor functions ( point-x, point-y ) –Type predicate ( point-p ) New structures are new types

Lecture 6-2CS250: Intro to AI/Lisp Default Values for Structure Fields Add a default value (defstruct midterm (difficulty (progn (format t “How hard was it?”) (read))) (max-grade 54) (num-completed nil))

Lecture 6-2CS250: Intro to AI/Lisp Customize Automatic Functions (defstruct (point (:conc-name p) (:print-function print-point)) (x 0) (y 0)) (defun print-point (p stream depth) (format stream “# ” (px p) (py p)))