Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn.

Slides:



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

09 Examples Functional Programming. Tower of Hanoi AB C.
CS 63 LISP Philip Greenspun's Tenth* Rule of 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 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)
TUTORIAL 3 CSCI3230 ( First Term) By Leo LIU Paco WONG CAO Qin 1 Hands on.
Lisp Recitation (cse471/598 Fall 2007 ) Aravind Kalavagattu.
CSE 341, S. Tanimoto Pattern Matching - 1 Pattern Matching in Lisp Lists can be used to represent sentences, relations, tree structures, etc. (this list.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
Common Lisp! John Paxton Montana State University Summer 2003.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
CSE S. Tanimoto Macros 1 Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
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.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
It is suggested that you use the Lisp interpreter available on the general machine (general.asu.edu). You are welcome to use other interpreters while developing,
LISP A brief overview. Lisp stands for “LISt Process” –Invented by John McCarthy (1958) –Simple data structure (atoms and lists) –Heavy use of recursion.
CSE 341, S. Tanimoto Lisp Data Structures - 1 Data Structures in Lisp 0. Collections of associations, association lists. 1. Creating graphs with conses.
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.
Input/Output Chapters 7 & 9. Output n Print produces output > (print 100) n It also returns the value it printed –that’s where the second 100 came.
COMMON LISP.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
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.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Artificial Intelligence IES 503 Asst. Prof. Dr. Senem Kumova Metin.
06 INPUT AND OUTPUT Functional Programming. Streams Two kinds of streams  Character streams  Binary streams Character streams are Lisp objects representing.
Lecture 1-2CS251: Intro to AI/Lisp II “And now for something completely different…”
Macros “How can you get anything done in [other languages], I think, without macros?” - Paul Graham, 2003.
CSE S. Tanimoto Lisp Defining Macros in Lisp Extensibility: A language is extensible if the language can be extended. New Lisp control structures.
More Language Features 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.
Design of Problem Solvers (PS) using Classical Problem Solving (CPS) techniques Classical Problem Solver has 2 basic components:  Search engine (uses.
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.
Basic Introduction to Lisp
CSE 341, S. Tanimoto Lisp Explicit Application of Functions and Functional Arguments In Lisp, functions can be passed as arguments to other functions.
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.
Section 15.4, 15.6 plus other materials
Defining Macros in Lisp
Example of formula (defun roots (a b c) (list
Data Structures in Lisp
Getting Started with Lisp
LISP A brief overview.
Using Lisp Lisp is a interactive system
PZ10CX - LISP Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section Appendix A.6.
First Lecture on Introductory Lisp
Modern Programming Languages Lecture 21 Fakhar Lodhi
Lisp Tutorial Click on Xlisp icon – you enter the interpreter
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
Defining Macros in Lisp
Defining Functions with DEFUN
Abstraction and Repetition
Functional Programming: Lisp
Lisp: Using Functions as Data
Data Structures in Lisp
Data Structures in Lisp
Common Lisp II.
Presentation transcript:

Lisp Files, Arrays, and Macros CIS 479/579 Bruce R. Maxim UM-Dearborn

input “read” is equivalent to “cin” in C++ Reads an entire expression from the keyboard when called Typical use (setq A (read)) Generally wise to prompt the user for input first, since “read” gives no indication that it is looking for input

output (prin1 "sam") "sam" > (princ "sam") sam "sam" > (print "sam") "sam" > (terpri) nil

output > (defun out () (print "sam")(prin1 "sam")(princ "sam") ) out > (out) "sam" "sam"sam "sam" > (progn (princ "sam") (terpri)) sam nil > (progn (princ "sam") t) sam t

User Interaction (defun make-graph () (princ "function to graph? ") (setq func (read)) (princ "starting value? ") (setq start (read)) (princ "ending value? ") (setq end (read)) (princ "plotting symbol? ") (setq sym (read)) (plot-points sym (mapcar func (generate start end))) (princ " "))

Plotting Output > (make-graph) FUNCTION TO GRAPH? square STARTING VALUE? -3 ENDING VALUE? 3 PLOTTING SYMBOL? umd umd "

Plotting Example (defun space (n) (cond (( m n) nil) (t (cons m (generate (1+ m) n))))) (defun square (x) (* x x))

format > (format t "the symbol ~s appeared ~s times ~%" 'a 3) the symbol a appeared 3 times nil > (format t "~% Hello ~a old boy" "bruce") Hello bruce old boy nil > (format t "~% Hello ~a old boy" '(a b c)) Hello (a b c) old boy nil

File Input > (setq fp (open "plot.lsp" :direction :input)) # > (read fp nil) (defun space (n) (cond ((< n 0) (princ "ERROR!")) ((zerop n) nil) (t (princ " ") (space (1- n))))) > (setq a (read fp nil)) ") (SPACE (1- N))))) > (close fp) t

Displaying File Contents (do* ((fp (open "plot.lsp" :direction :input)) (ex (read fp nil) (read fp nil)) ) ((null ex) nil) (print ex) )

plot.lsp Contents (defun space (n) (cond ((< n 0) (princ "ERROR!")) ((zerop n) nil) (t (princ " ") (space (1- n))))) (defun plot-one-point (sym val) (space val) (princ sym) (terpri)) (defun plot-points (sym ylis) (mapcar (function (lambda (y) (plot-one-point sym y))) ylis)) (defun generate (m n) (cond ((> m n) nil) (t (cons m (generate (1+ m) n))))) (defun square (x) (* x x)) (defun make-graph nil (princ "FUNCTION TO GRAPH? ") (setq func (read)) (princ "STARTING VALUE? ") (setq start (read)) (princ "ENDING VALUE? ") (setq end (read)) (princ "PLOTTING SYMBOL? ") (setq sym (read)) (plot-points sym (mapcar func (generate start end))) (princ " ")) nil

File Copy defun copy (fn1 fn2) (do* ((old (open fn1 :direction :input)) (new (open fn2 :direction :output)) (ex (read old nil) (read old nil)) ) ((null ex) (close old) (close new)) (print ex new) ) copy > (copy "plot.lsp" "temp.txt") t

Copied File is Executable > (make-graph) FUNCTION TO GRAPH? square STARTING VALUE? -2 ENDING VALUE? 2 PLOTTING SYMBOL? * * "

Batch File Processing > (defun batch (fn1 fn2) (do* ((old (open fn1 :direction :input)) (new (open fn2 :direction :output)) (ex (read old nil) (read old nil)) ) ((null ex) (close old) (princ "end of program" new) (terpri new) (close new) ) (print ex new) (print (eval ex) new) (terpri new) ) batch

Batch File Processing > (batch "test.txt" "out.txt") t Contents of “test.txt” (car '(a b c)) (* 2 3) (cons 'a '(x y z))

Batch File Processing Contents of “out.txt” (car (quote (a b c))) a (* 2 3) 6 (cons (quote a) (quote (x y z))) (a x y z) end of program

List Surgery > (setq a '(a b c)) (a b c) > (setq b '(d e f)) (d e f) > (append a b) (a b c d e f) > (nconc a b) (a b c d e f) > a (a b c d e f) > b (d e f)

List Surgery (setq abc '(a b c)) (a b c) > (setq xyz '(x y z)) (x y z) > (setq bc (cdr abc)) (b c) > (setq yz (cdr xyz)) (y z) > (setq a (nconc abc xyz)) (a b c x y z)

List Surgery (> a (a b c x y z) > abc (a b c x y z) > xyz (x y z) > bc (b c x y z) > yz (y z)

List Surgery > (setq a '(a b c d e)) (a b c d e) > (rplaca a '(a b)) ((a b) b c d e) > a ((a b) b c d e) > (rplacd a '(x y z)) ((a b) x y z)

List Surgery > (setq a '(a b c d e f)) (a b c d e f) > (delete 'a a) (b c d e f) > a (a b c d e f) > (delete 'e a) (a b c d f) > a (a b c d f) > (setq a (delete 'a a)) (b c d f) > a (b c d f) > (delete 'a '(a b a c a d a e)) (b c d e)

Arrays > (setq arr (make-array 3)) #(nil nil nil) > (setf (aref arr 2) 4) 4 > arr #(nil nil 4) > (aref arr 1) nil > (setq a 3) 3

Backquote > '(if a is true (and a a) then) (if a is true (and a a) then) > `(if,a is true,(and a a ) then) (if 3 is true 3 then) > (list 'if a 'is 'true (list a a) 'is 'true) (if 3 is true (3 3) is true) > (list 'if a 'is 'true (and a a) 'is 'true) (if 3 is true 3 is true) > (setq b '(a b c)) (a b c) > `(hello fred,b) (hello fred (a b c)) > `(hello (hello fred a b c)

Macros > (defun name (x) (car x)) name > (name '(a red table)) a > (defmacro name (x) `(car,x)) name > (name '(a red table)) a > (defmacro name (x) (list 'car x)) name > (name '(a red table)) a

Macros > (defmacro sq (var val) `(setq,var,val)) sq > (defun sq-test (a b) (sq x a) (sq y b) ) sq-test > (sq-test 3 4) 4 > x 3 > y 4

Macros > (defmacro if2 (a b c) `(cond (,a,b) (t,c) ) if2 > (if2 (atom x) 'yes 'no) yes

&optional > (defmacro if2 (a b &optional c) `(cond (,a,b) (t,c) ) if2 > (if2 (atom x) 'yes 'no) yes > (if2 (atom x) 'yes) yes

&rest > (defun list2 (&rest x) x) list2 > (list ) ( ) > (defmacro let*2 (x &rest forms) (if (null x) `(let (,(car x)) (let*2,(cdr ) let*2 >