1 More About Lisp. 2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1.

Slides:



Advertisements
Similar presentations
ANSI Common Lisp 3. Lists 20 June Lists Conses List Functions Trees Sets Stacks Dotted Lists Assoc-lists.
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.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages and Paradigms Lisp Programming.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
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.
Procedures with optional parameters which do not require matching arguments Example: consider the exponent function which may take one argument, m, in.
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.
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)
Introductory Lisp Programming Lecture # 2 Main Topics –Basic Lisp data types –Lisp primitives –Details of list handling Cons cells (boxes) & their representation.
TES3111 Oct 2001 Data Structures S-Expression - Symbolic expression. It can be an Atom, a List or a collection of S- Expression enclosed by (…) Atom -
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.
Allegro CL Certification Program Lisp Programming Series Level I Session Top Ten Things to Know.
Yu-Tzu Lin ( 林育慈 )
Functional Programming
Functional Programming 02 Lists
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.
A Variable is symbolic name that can be given different values. Variables are stored in particular places in the computer ‘s memory. When a variable is.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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.
Common lisp A functional programming language. Useful URL:
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 6-2CS250: Intro to AI/Lisp Programming in Your Favorite Language Lecture 5-2 February 11 th, 1999 CS250.
Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University.
Predicates, Functions and Files "I suppose I should learn Lisp, but it seems so foreign." - Paul Graham, Nov 1983.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Building user-defined functions: the progressive envelopment technique The idea: define combinations of LISP primitives through a sequence of experiments.
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.
Control in LISP More on Predicates & Conditionals.
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.
Introduction to LISP Atoms, Lists Math. LISP n LISt Processing n Function model –Program = function definition –Give arguments –Returns values n Mathematical.
PRACTICAL COMMON LISP Peter Seibel 1.
Chapter 14 LISP – Practical 3 Instructor: Haris Shahzad Artificial Intelligence CS-402.
Input/Output Chapters 8 & 9. Character Input n Read-char reads a single character n Read-line reads until the next end-of-line –returns a string n Both.
EZGİ GENÇ History Lisp is a family of computer programming languages with a long history and a distinctive, fully parenthesized syntax. Originally.
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
Intelligent Agents Lecture 3-2 October 14th, 1999 CS250 Lecture 2-1
Getting Started with Lisp
Lists in Lisp and Scheme
LISP A brief overview.
First Lecture on Introductory Lisp
J.E. Spragg Mitthögskolan 1997
Imperative Data Structures
Modern Programming Languages Lecture 21 Fakhar Lodhi
Allegro CL Certification Program
Modern Programming Languages Lecture 20 Fakhar Lodhi
LISP A brief overview.
John McCarthy Pioneer in AI Also Lisp Formalize common-sense reasoning
Data Structures in Lisp
Data Structures in Lisp
Modern Programming Languages Lecture 18 Fakhar Lodhi
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:

1 More About Lisp

2 Functions on Lists (setf x '(a b c)) => (A B C) (setf y '(1 2 3)) => (1 2 3) (rest x) => (B C) ;;all but the first element (nth 1 x) => B ;;nth element of a list (car x) => A ;;the first element of a list (cdr y) => (2 3) ;;all but the first name (reverse x) => (C B A) ;;puts list in reverse order (cons 0 y) => ( ) ;;add to front of list (append x y) => (A B C 1 2 3) ;;append together elements (list x y) => ((A B C) (1 2 3)) ;;make a new list (list* x y) =>((A B C) 1 2 3) ;;append last argument to others (null nil) => T ;;it is true for the empty list (null x) => NIL ;;it is false for everything else (listp x) => T ;;it is true for any list (listp nil) => T ;;including nil list (listp 3) => NIL ;;it is false for non-list (consp x) => T ;;it is true for non-nil list (consp nil) => NIL ;;it is false for atoms, including nil (equal x y) => NIL ;;false for lists that looks different (equal x '(a b c)) => T ;;true for lists that look the same (sort y #'>) => (3 2 1) ;;sort the list according to > function (subseq x 1 2) => (B) ;;subsequence with given start and end

3 Different equality functions eq – tests for the exact same object eql – tests for objects that are equivalent numbers equal – tests for objects that are lists or strings with eql elements equalp – likes equal except it matches upper- and lowercase characters and numbers of different types strict x y eq eql equal equalp ‘x ‘x ‘0 ‘0 ‘(x) ‘(x) ‘”xy” ‘”xy” ‘”Xy” ‘”xY” ‘0 ‘0.0 T T T T nil nil T T nil nil nil T Others: = tree-equal char-equal string-equal

4 Functions on Tables ;; table representation (setf capitalCity-table ‘ ((UK. London) (JP. Tokyo) (CN. Beijing) (UAS. Washingto n DC)) ) ((UK. LONDON) (JP. TOKYO) (CN. BEIJING) (UAS. WASHINGTONDC)) (assoc 'JP capitalCity-table) => (JP. TOKYO) (cdr (assoc 'JP capitalCity-table)) => TOKYO (assoc 'FR capitalCity-table) => NIL (rassoc 'Tokyo capitalCity-table) => (JP. TOKYO) ;; hash table representation (setf hashTable (make-hash-table)) => # (setf (gethash 'UK hashTable) 'London) => LONDON (setf (gethash 'JP hashTable) 'Tokyo) => TOKYO (setf (gethash 'CN hashTable) 'BeiJing) => BEIJING (setf (gethash 'USA hashTable) 'WashingtonDC) => WASHINGTONDC (gethash 'UK hashTable) => LONDON T (gethash 'FR hashTable) => NIL NIL ;;property list (property/value) representation (setf (get 'UK 'capital) 'London) => LONDON (setf (get 'JP 'capital) 'Tokyo) => TOKYO (setf (get 'CN 'capital) 'BeiJing) => BEIJING (setf (get 'USA 'capital) 'WashingtonDC) => WASHINGTONDC (get 'UK 'capital) => LONDON (get 'FR 'capital) => NIL

5 Functions on Trees (setf aTree '((a b) ((c)) (d e))) ((A B) ((C)) (D E)) (tree-equal aTree '((a b) ((c)) (d e))) => T (tree-equal aTree '((1 2) ((3)) (4 5 ))) => NIL (defun same-shape-tree (a b) "Are two trees the same except for the leaves?" (tree-equal a b :test #'true)) SAME-SHAPE-TREE (defun true (&rest ignore) t) TRUE (same-shape-tree aTree '((1 2) ((3)) (4 5 ))) => T (same-shape-tree aTree '((1 2) ((3)) (4 5 6))) => NIL (subst '1 ‘c aTree) ((A B) ((1)) (D E)) (sublis '((a. 1)) aTree) ((A B) ((1)) (D E)) (defun english->french (words) (sublis '((are. va) (friend. ami) (hello. bonjour) (how. comment) (my. mon) (you. tu) ) words) ) ENGLISH->FRENCH (english->french '(hello my friend - how are you today?)) (BONJOUR MON AMI - COMMENT VA TU TODAY?)

6 Functions on Numbers (+ 4 2) => 6 (- 4 2) => 2 (* 4 2) => 8 (/ 4 2) => 2 (> ) => T (= ) => NIL ( T (random 100) => 68 (expt 4 2) => 16 (sin pi) => 0.0 (asin 0) => 0.0 (min 2 3 4) => 2 (max 4 5 2) => 5 (abs -3) => 3 (sqrt 4) => 2.0 (round 4.1) => 4 (rem 11 5) => 1 (setf r '(a b c d)) => (A B C D) (setf s '(c d e)) => (C D E) (intersection r s) =>(C D) (union r s) => (A B C D E) (set-difference r s) => (A B) (member 'd r) => (D) (subsetp s r) => NIL (adjoin 'b s) => (B C D E) (adjoin 'c s) => (C D E) (bit-and #*11110 #*11001) => #*11000 (logand #b11110 #b11001) => 24 Functions on Sets Others: logior, bit-ior logandc2, bit-andc2 logbitp, bit logcount

7 Data Types Type Example Explanation character # c number 42 float 3.14 integer 24 fixnum 123 An integer that fits in a single word of storage bignum An integer of unbounded size function #’sin A function can be applied to an argument list symbol sin Symbols can name fns and vars, and are themselves objects null nil keyword :key Keywords are a subtype of symbol sequence (a b c) Including lists and vectors list (a b c) A cons or null vector #(a b c) a subtype of sequence cons (a b c) a non-nil list atom t anything that is not cons string “a b c” array #1A(a b c) vectors and higher-dimension arrays structure #S(type …) Structures are defined by defstruct hash-table It is created by make-hash-table (setf ar #1A(a b c)) => #(A B C) (aref ar 1) => B (setf ar #2A((a b c) (d f g))) #2A((A B C)(D F G)) (aref ar 1 2) => G (defstruct name first last) NAME (setf st #S(name :first rose :last huang)) #S( NAME :FIRST ROSE :LAST HUANG )

8 Data Types Type Example Explanation t 52 Every object is of type t nil No object is of type nil complex #C(0 1) Imaginary numbers bit 0 0 or 1 rational 2/3 Including integers and ratios ratio 2/3 Exact fractional numbers simple-array #1A(x y) An array that is not displaced or adjustable readtable A mapping from characters to their meanings to read package A collection of symbols that form a module pathname #P”/usr/spool/mail” A file or directory name stream A pointer to an open file random-state A state used as a seed by random

9 Input/Output print prints any object on a new line. prin1 prints any object without the new line and space. princ is used to print in a human-readable format. read for print and prin1, the object is printed in a form that could be processed by read. for princ, a string is interpreted as symbols rather a string by read. write accepts eleven different keyword arguments that controls whether it acts like prin1 or princ. (with-open-file (stream "test.text" :direction :output) (print '(hello there) stream) (princ 'goodbye stream)) GOODBYE (with-open-file (stream "test.text":direction :input) (list (read stream) (read-char stream) (read-char stream) (read-char stream) (read stream) (read stream nil 'eof))) ((HELLO THERE) G O ODBYE EOF) In file, test.text: (HELLO THERE) GOODBYE If you insert a line here (terpri stream) What is result in test.text file?

10 Format terpri stands for “terminate print line” and it skips to the next line. fresh-line also skips to the next line, unless it can be determined that the output is already at the start of a line. format provides a very general function for doing formatted output. t is the first argument of format function. There are 26 different format directives ~ & moves to a fresh line ~ a prints the next argument as princ would. ~ s prints the next argument as prinl would. ~ f prints a number in float. ~ r prints the next argument, which should be a number, in English. prints a number as a roman numeral. ~ {…~} takes the next argument, which must be a list, and formats each element of the list according to the format string inside the braces. ~ ^ exits from the enclosing ~ {…~} loop if there are no more arguments.

11 Examples of format : (format t "Hello, how are you?") two plus "two" is 4.0NIL (format t "~&~a plus ~s is ~f" "two" "two" 4) hello! how are you?NIL (let ((numbers '( ))) (format t "~&~{~r~^ plus ~} is ~f" numbers (apply #'+ numbers)) ) one plus two plus three plus four plus five is 15.0NIL

12 (defun f-to-c () (format t "~%Please enter Fahrenheit temperature: ") (let* ((ftemp (read)) (ctemp (* (- ( first ftemp ) 32) 5/9))) (format t "~%~s degrees Fahrenheit is ~s degrees Celsius~%" ftemp (float ctemp)) ;; print floated value ctemp)) ;; return ratio value F-TO-C (f-to-c) Please enter Fahrenheit temperature: (100) (100) degrees Fahrenheit is degrees Celsius 340/9 e.g. 1: temperature converter

13 (defun math-quiz (op range n) " ask the user a series of math questions. " (dotimes (i n) (question (random range) op (random range)))) MATH-QUIZ (defun question(x op y) " ask a math question, read a reply, and say if it is correct. " (format t "~&How much is ~d ~a ~d?" x op y) (if (eql (read) (funcall op x y)) (princ "correct!") (princ "sorry, that is not right."))) QUESTION (math-quiz ' ) How much is ?(113) correct! How much is ?(107) correct! How much is ?(40) sorry, that is not right.NIL e.g. 2: math quiz

14 Exercise Problems If you have time, try to do the following exercise problems. 1.Write a function that can save (into a file) and print nine-nine table ( 九九表 ) in the table format. 2.Modify the example 2 (e.g.2: math-quiz) in lecture note so that users does not need to remember three arguments.