1 Introduction of Lisp. 2 + - function e.g. (+ 2 2) Shift+Enter Execute …… (+ 1 2) 3 (+ 1 2 3 4 5) 15 (- 1 6) -5 (- 10 1 2 3 4 5) -5 (- (+ 800 80 8) (+

Slides:



Advertisements
Similar presentations
CS 63 LISP Philip Greenspun's Tenth* Rule of Programming:
Advertisements

Some non-recursive tricks. The Lambda expression. More on Let, Let*, apply and funcall.
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.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
Lisp – Introduction יעל נצר מערכות נבונות סמסטר ב' תשס"ו.
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.
Common Lisp! John Paxton Montana State University Summer 2003.
Common Lisp! John Paxton Montana State University Summer 2003.
LISP primitives on sequences FIRST (or CAR) and REST (or CDR) take lists apart. Consider the list (First day of the semester). * (first '(First day of.
>(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?
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.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Recursion, Data Abstraction, Mapping, Iteration.
Symbolic Expressions (S Expressions) Syntax: Opening and Closing parenthesis having elements in between. List represented in LISP: (A B2 C3 Shahid) (A.
Functional Programming COMP2003 A course on functional programming using Common Lisp Dr Eleni Mangina
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.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
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.
Yu-Tzu Lin ( 林育慈 )
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.
Alok Mehta - Programming in Lisp - Data Abstraction and Mapping Programming in Lisp Data Abstraction and Mapping.
The Case primitive: matches the evaluated key form against the unevaluated keys by using eql The general format of case is the following: (case (... ).....
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.
Introduction to Lisp For Scheme Users. What Makes Lisp Different? Built-in Support for Lists Automatic Storage Management Dynamic Typing First-Class Functions.
Common lisp A functional programming language. Useful URL:
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
Iteration Chapters 6 & 7. Iteration in LISP n LISP (unlike Prolog) allows iteration –mapcar, remove-if(-not), count-if, find-if for special purpose iteration.
Basic LISP Programming Common LISP follows the algorithm below when interacting with users: loop read in an expression from the console; evaluate the expression;
Allegro CL Certification Program Lisp Programming Series Level I Session Basic Lisp Development in the IDE.
Programming in Lisp; Instructor: Alok Mehta Programming in Lisp Introduction to Lisp.
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.
You can access the members of a list with the functions car (or first) and cdr (or rest): (setf list '(a b c)) (car list) ⇒ a (first list) ⇒ a (cdr list)
Operating on Lists Chapter 6. Firsts and Seconds n Transforming list of pairs into two lists –(firsts ‘((1 5) (2 6) (3 7)))  (1 2 3) –(seconds ‘((1 5)
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.
Basic Introduction to Lisp
CSE S. Tanimoto Lisps's Basic Functionality 1 LISP: Basic Functionality S-expressions Conses Lists Predicates Evaluation and quoting Conditional.
1 Simple Lisp Programs References:
1 Outline Review Introduction to LISP Symbols and Numbers Lists Writing LISP Functions LISPWorks.
Section 15.4, 15.6 plus other materials
Lisp S-Expressions: ATOMs
LISP – (LISt Processing)
LISP A brief overview.
FP Foundations, Scheme In Text: Chapter 14.
Scheme: Basic Functionality
Modern Programming Languages Lecture 20 Fakhar Lodhi
CSE S. Tanimoto Explicit Function Application
Lisp: Representation of Data
Defining Functions with DEFUN
Abstraction and Repetition
LISP: Basic Functionality
Modern Programming Languages Lecture 18 Fakhar Lodhi
LISP: Basic Functionality
Common Lisp II.
LISP: Basic Functionality
Lisp: Representation of Data
The general format of case is the following: (case <key form>
List manipulation Consider student database, where each student is represented by the following list: * (setf student1 '((Paul Bennett) ((hw1 4.3) (hw2.
LISP primitives on sequences
Presentation transcript:

1 Introduction of Lisp

2 + - function e.g. (+ 2 2) Shift+Enter Execute …… (+ 1 2) 3 ( ) 15 (- 1 6) -5 ( ) -5 (- ( ) ( )) ; guess result? 444 Notes: ; this is a comment. Shift+Enter is execute key..

3 Symbolic Computation e.g. (append ’(dog cat) ’(rabbit)) Shift+Enter Execute …… (append '(tiger lion) (list '(dog cat) 'snake)) (TIGER LION (DOG CAT) SNAKE) (length (append '(tiger lion) (list '(dog cat) 'snake))) 4 '(+ 1 2) 3 Note: ’ a single quote: to mark the beginning of an expression. append, length, + are defined functions in Common Lisp. Symbols in Common Lisp are not case sensitive. A wide variety of characters are allowed in symbols.

4 Variables – define new objects in terms of others e.g. (setf p ’(Runhe Huang))Shift+Enter Execute …… (setf p '(Runhe Huang)) (RUNHE HUANG) p (RUNHE HUANG) (setf x 20) 20 (* x x) 400 (+ x (length p)) 22 pi d0 Note: after assigning a value to the variable named p we can refer to the value with the name p. Note: pi does not name a function but is a variable whose value is …

5 Special Forms Operators: defun (defun factorial(x) "returns the factorial of N, where N >=1" (if (= x 1) 1 (* x (factorial (- x 1))))) FACTORIAL (factorial 10) setf (setf x (+ 1 2)) 3 Other operators: defparameter, let, case, if, function(#’), quote(’)

6 Lists Functions (operating on lists) (setf p '(I am a student of Hosei University)) (I AM A STUDENT OF HOSEI UNIVERSITY) p (I AM A STUDENT OF HOSEI UNIVERSITY) (first p) I (last p) (UNIVERSITY) (rest p) (AM A STUDENT OF HOSEI UNIVERSITY) (second p) AM (third p) A (eighth p) NIL (length p) 7 p becomes a list first, second, third, eighth, length are the functions that can operate on a list. There is more …

7 Try more … (access parts of lists) (setf x '((1st element) 2 (element 3) ((4)) 5 )) (length x) (first x) (first (fourth x)) (first (first (fourth x))) (second (first x)) Try more … (build up new lists) (setf p '(Runhe Rose Huang)) (RUNHE ROSE HUANG) (cons 'Dr p) (DR RUNHE ROSE HUANG) (setf affliation (list 'Hosei 'Univeristy)) (HOSEI UNIVERISTY) (list p 'is 'working 'at affliation) ((RUNHE ROSE HUANG) IS WORKING AT (HOSEI UNIVERISTY)) cons stand for “construct”.

8 Defining new functions Form: (defun function-name (parameter…) “documenttaion string” function-body) Form: (defun function-name (parameter…) “documenttaion string” function-body) (defun factorial(x) "returns the factorial of N, where N >=1" (if (= x 1) 1 (* x (factorial (- x 1))))) (defun first-name(name) " Select the first name from a name represented as a list " (first name)) (factorial 10) => (setf p '(Runhe Huang) ) => (RUNHE HUANG) (first-name p) => RUNHE

9 Built-in function: mapcar Usage: (setf names '( (Runhe Rose Huang) (Jianhua John Ma)(Abe Suzuki) (Mr Mike Steven) )) ( (RUNHE ROSE HUANG) (JIANHUA JOHN MA) (ABE SUZUKI) (Mr MIKE STEVEN) ) (mapcar #'first-name names) (RUNHE JIANHUA ABE MR) Usage: (setf names '( (Runhe Rose Huang) (Jianhua John Ma)(Abe Suzuki) (Mr Mike Steven) )) ( (RUNHE ROSE HUANG) (JIANHUA JOHN MA) (ABE SUZUKI) (Mr MIKE STEVEN) ) (mapcar #'first-name names) (RUNHE JIANHUA ABE MR) (list (first-name (first names)) (first-name (second names)) (first-name (third names)) (first-name (fourth names))) (RUNHE JIANHUA ABE MR) The built-in function mapcar is passed two arguments, a function (#'first-name) a list (names) #’ notation maps from name of a function to the function itself. equivalent to

10 Test function using mapcar (mapcar #'last-name names) ;;; An error occurred in function _UNBOUND- FUNCTION-ERROR: ;;; Error: The symbol LAST-NAME does not have a function binding ;;; Entering Corman Lisp debug loop. ;;; Use :C followed by an option to exit. Type :HELP for help. ;;; Restart options: ;;; 1 Abort to top level. (mapcar #'- '( )) ( ) (mapcar #'+ '( ) '( )) ( ) (mapcar #'factorial '( ) ) ( ) Some more example of mapcar

11 Special form: defparameter and if Notes: member is a built-in function that tests to see if its first argument ((first name)) is an element of the list passed as the second argument (*title*). Some more example of mapcar Examples: (defparameter *titles*) '(Mr Mrs Miss Ms Sir Madam Dr Prof) " A list of titles that can appear at the start of a name ") *TITLES* Examples: (defparameter *titles*) '(Mr Mrs Miss Ms Sir Madam Dr Prof) " A list of titles that can appear at the start of a name ") *TITLES* (defun first-name (name) "Select the first name from a name list" (if (member (first name) *titles*) (first-name (rest name)) (first name))) FIRST-NAME (defun first-name (name) "Select the first name from a name list" (if (member (first name) *titles*) (first-name (rest name)) (first name))) FIRST-NAME (if test then-part else-part) It is a recursive function

12 A complete example (setf names '( (Runhe Rose Huang) (Jianhua John Ma) (Abe Suzuki) (Mike Steven Morgan) )) ( (RUNHE ROSE HUANG) (JIANHUA JOHN MA) (ABE SUZUKI) (MIKE STEVEN MORGAN) ) (defparameter *titles* '(Mr Mrs Miss Ms Sir Madam Dr Prof) "A list of titles that can appear at the start of a name" ) *TITLES* (defun first-name (name) "Select the first name from a name list" (if (member (first name) *titles*) (first-name (rest name)) (first name))) FIRST-NAME (mapcar #'first-name names) (RUNHE JIANHUA ABE MIKE) MR is replaced by MIKE

13 Built-in function: trace and untrace (trace first-name) (FIRST-NAME) (first-name '(Dr Mrs Rose Huang)) Entering function FIRST-NAME with params ((DR MRS ROSE HUANG)) Function FIRST-NAME returned value(s) (ROSE) ROSE (untrace first-name) NIL (first-name '(Dr Mrs Rose Huang)) ROSE (trace first-name) (FIRST-NAME) (first-name '(Dr Mrs Rose Huang)) Entering function FIRST-NAME with params ((DR MRS ROSE HUANG)) Function FIRST-NAME returned value(s) (ROSE) ROSE (untrace first-name) NIL (first-name '(Dr Mrs Rose Huang)) ROSE Notes: Using trace built-in function, you can get detailed tracing process although the value returned is same, ROSE.

14 Higher-order functions e.g. (mapcar #'+ '( ) '( )) (mapcar #'factorial '( ) ) e.g. (mapcar #'+ '( ) '( )) (mapcar #'factorial '( ) ) A function that takes another function as an argument is called a higher-order function. mapcar is a higher-order function since #'+ and #'factorial are functions as arguments passed to the function, mapcar. e.g. (apply #'+ '( )) 20 (apply #'append '(( )(a b c d))) ( A B C D) e.g. (apply #'+ '( )) 20 (apply #'append '(( )(a b c d))) ( A B C D) Notes: Both + and #'append are two built-in functions. apply is also a built-in higher-order function.

15 Try to understand … (defun mappend (fn the-list) "Apply fn to each element of list and append the results." (apply #'append (mapcar fn the-list))) MAPPEND (defun self-and-double (x) (list x (+ x x))) SELF-AND-DOUBLE (mapcar #'self-and-double '( )) ((1 2) (10 20) ( )) (mappend #'self-and-double '( )) ( ) Can you define a higher-order function and execute it?

16 Exercise Problems If you have time, try to do the following exercise problems. 1.Apply square function, sqr, to each element of list and append the results. hints: this is exercise of a higher-order function. you need to define functions, mappend , sqr, and execute the function, mappend, that takes two arguments, the function, sqr, and a list.

17 (defun mappend (fn the-list) "Apply fn to each element of list and append the results." (apply #'append (mapcar fn the-list))) MAPPEND (defun sqr(x) (list x (* x x))) SQR (mappend #'sqr '( )) ( ) (mapcar #'sqr '( )) ((1 1) (10 100) ( )) Solution to Ex2.