Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn.

Slides:



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

CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
Heuristic Search CIS 479/579 Bruce R. Maxim UM-Dearborn.
1 Search Problems (read Chapters 3 and 4 of Russell and Norvig) Many (perhaps most) AI problems can be considered search problems. This can be modeled.
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.
ANSI Common Lisp 5. Control 16 June Blocks -- progn progn > (progn (format t “a”) (format t “b”) ( )) ab 23 The expressions within its body.
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.
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 COSC3306: Programming Paradigms Lecture 11: Applicative Programming with Lisp Haibin Zhu, Ph.D. Computer Science Nipissing University (C) 2003.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
ITERATIVE CONSTRUCTS: DOLIST Dolist is an iterative construct (a loop statement) consisting of a variable declaration and a body The body states what happens.
Introduction to Artificial Intelligence Lisp Ruth Bergman Fall 2002.
First Lecture on Introductory Lisp Yun Peng. Why Lisp? Because it’s the most widely used AI programming language Because AI researchers and theoreticians.
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)
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
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.
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.
Functional Programming
Functional Programming 02 Lists
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
Control Structures We have already visited some of the control structures (if, if-else, dolist, dotimes) –Here we take a more in-depth look at control.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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.
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.
KU NLP Artificial Intelligence1 Ch 15. An Introduction to LISP q 15.0 Introduction q 15.1 LISP: A Brief Overview  Symbolic Expressions, the Syntactic.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
04 Control. Control-Blocks Common Lisp has 3 basic operators for creating blocks of code progn block tagbody If ordinary function calls are the leaves.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.
More Language Features Functional Programming Academic Year Alessandro Cimatti
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.
PRACTICAL COMMON LISP Peter Seibel 1.
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.
Functional Programming: Lisp MacLennan Chapter 10.
Comparative Programming Languages Functional programming with Lisp/Scheme.
Common LISP VS JAVA 김민철. Table of contents  1. LISP …………………………………………… 3  2. 데이터 타입 …………………………………… 4  3. 산술 및 논리 연산 ……………………………… 8  4. 변수 선언 및 할당 ………………………………
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Additional Scheme examples
Section 15.4, 15.6 plus other materials
Example of formula (defun roots (a b c) (list
Modern Programming Languages Lecture 20 Fakhar Lodhi
Getting Started with Lisp
Functional Languages Early in AI research, there was a need for symbolic computing handling symbols instead of numbers or strings recursion Design goals.
LISP A brief overview.
Using Lisp Lisp is a interactive system
First Lecture on Introductory Lisp
Functional Languages Early in AI research, there was a need for symbolic computing handling symbols instead of numbers or strings parsing input recursion.
J.E. Spragg Mitthögskolan 1997
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Common Lisp II.
The general format of case is the following: (case <key form>
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
Presentation transcript:

Lisp Control and Data Structures CIS 479/579 Bruce R. Maxim UM-Dearborn

and (and nil t t) nil > (and t nil t) nil > (and 1 2 3) 3 > (defun sign (a b) (and (oddp a) (oddp b) 'both-odd) ) sign > (sign 2 3) nil > (sign 3 5) both-odd

or > (or t nil t) t > (or 1 2 3) 1 > (or 'george nil 'harry) george > (defun same-sign (x y) (or (and (zerop x) (zerop y)) (and (< x 0) (< y 0)) (and (> x 0) (> y 0)) ) same-sign > (same-sign 0 0) t > (same-sign -1 3) nil

cond > (defun comp (op x y) (cond ((equal op 'sum-of) (+ x y)) ((equal op 'prod-of) (* x y)) (t '(does not compute)) ) comp > (comp 'sum-of 2 4) 6 > (comp 'larry 2 3) (does not compute)

if and case > (if (listp 1) (car 1) '(not a list) ) (not a list) > (setq b 'c) c > (case B ('a '1st) ('b '2nd) ('c '3rd) ) 3rd

let > (defun aug (first second) (let ((item first) (bag second) ) (cond ((listp first) (setq item second) (setq bag first)) ) (if (member item bag) bag (cons item bag)) ) aug > (aug 'a '(a b c)) (a b c) > (aug '(a b c) 'a) (a b c)

let* Please remember let does not allow you to declare local identifiers with initial values that depend on one another You need to use let* for these types of declarations (let* ((item (if (listp first) second first)) (bag (if (= item second) first second))... )

Recursive Functions m n = 1 for n = 0 m n = m * m n-1 for n > 0 > (defun expon (m n) (cond ((zerop n) 1) (t (* m (expon m (1- n))) ) expon > (expon 2 3) 8

car/cdr recursion > (defun cnt-atom (l) (cond ((null l) 0) ; empty list ((atom l) 1) ; not a list (t (+ (cnt-atom (car l)) (cnt-atom (cdr l)) ) cnt-atom > (cnt-atom '(a (b c) d (e f (g h)))) 8

trace and untrace > (trace cnt-atom) (cnt-atom) > (cnt-atom '(a b)) Entering: CNT-ATOM, Argument list: ((a b)) Entering: CNT-ATOM, Argument list: (a) Exiting: CNT-ATOM, Value: 1 Entering: CNT-ATOM, Argument list: ((b)) Entering: CNT-ATOM, Argument list: (b) Exiting: CNT-ATOM, Value: 1 Entering: CNT-ATOM, Argument list: (nil) Exiting: CNT-ATOM, Value: 0 Exiting: CNT-ATOM, Value: 1 Exiting: CNT-ATOM, Value: 2 2 > (untrace cnt-atom) nil

apply > (apply '+ '( )) 14 > ( ) 14 > (apply 'equal '(12 14)) nil > (apply 'cons '(as (you like it))) (as you like it)

mapcar > (mapcar 'oddp '( )) (t nil t nil) > (defun square (x) (* x x) ) square > (mapcar 'square '( )) ( )

mapcar > (setq words '((one eins) (two zwei) (three drei)) ) ((one eins) (two zwei) (three drei) > (mapcar 'car words) (one two three) > (mapcar 'cadr words) (eins zwei drei) > (mapcar 'cdr words) ((eins) (zwei) (drei))

other map functions > (mapcar 'reverse words) ((eins one) (zwei two) (drei three)) > (mapcan 'reverse words) (eins one zwei two drei three) > (maplist 'reverse words) (((three drei) (two zwei) (one eins)) ((three drei) (two zwei)) ((three drei))) > (mapcon 'reverse words) ((three drei) (two zwei) (one eins) (three drei) (two zwei) (three drei)) > (maplist 'cdr words) (((two zwei) (three drei)) ((three drei)) nil)

lambda functions > (mapcar #'square '( )) ( ) > (mapcar #'(lambda (x) (* x x)) '( )) ( ) > (mapcar #'(lambda (x) (car x) (cadr x)) words) (eins zwei drei) > (mapcan #'(lambda (x) x) words) (one eins two zwei three drei)

prog and loops > (defun expt (m n) (prog ((result 1) (expon n)) loop1 (if (zerop expon) (return result)) (setq result (* m result)) (setq expon (1- expon)) (go loop1) ) expt > (expt 2 5) 32

progn and prog1 > (progn (setq x 'foo) (setq x 'bar) (setq x 'baz) 'done) done > x baz > (prog1 (setq x 'foo) (setq x 'bar) (setq x 'baz) 'done) foo > x baz

do > (defun count (L) (do ((cnt 0 (1+ cnt)) (loaf L (cdr loaf)) ) ((null loaf) cnt) ) count > (count '(a (b c) d e)) 4

do > (defun fact (x) (do ((n 1 (1+ n)) (res 1) ) ((> n x) res) (setq res (* res n)) ) fact > (fact 6) 720

dolist and dotimes > (dolist (x '(a b c) y) (setq y (list x)) ) (c) > (dotimes (x 3 x) x) 3 > (dotimes (x 3 x) (print x) )

property lists > (putprop 'fred 'male 'sex) male > (get 'fred 'sex) male > (get 'fred 'height) nil > (setf (get 'fred 'sex) 'female) female > (get 'fred 'sex) female

property lists > (setf (symbol-plist 'fred) '(sex male age 23 sibs (bob carol))) (sex male age 23 sibs (bob carol)) > (get 'fred 'sibs) (bob carol) > (remprop 'fred 'sibs) nil > (symbol-plist 'fred) (sex male age 23) > (setq fred 10) 10 > fred 10 > (symbol-plist 'a) nil

strings > (char "sam eats soup" 5) #\a > (string 97) "a > (string #\a) "a" > (char "SAM EATS SOUP" 5) #\A > (strcat "a" "b" "c") "abc" > #\a #\a > (subseq "sam eats soup" 3 5) " e"