Download presentation
Presentation is loading. Please wait.
Published byMabel Randall Modified over 9 years ago
1
1 Introduction of Lisp
2
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) (+ 400 40 4)) ; guess result? 444 Notes: ; this is a comment. Shift+Enter is execute key..
3
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
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 3.141593d0 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 3.14159…
5
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) 3628800 setf (setf x (+ 1 2)) 3 Other operators: defparameter, let, case, if, function(#’), quote(’)
6
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
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
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) => 3628800 (setf p '(Runhe Huang) ) => (RUNHE HUANG) (first-name p) => RUNHE
9
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
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 #'- '(10 20 30 40)) (-10 -20 -30 -40) (mapcar #'+ '(2 4 6 8) '(10 20 30 40)) (12 24 36 48) (mapcar #'factorial '(2 4 6 8 10) ) (2 24 720 40320 3628800) Some more example of mapcar
11
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
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
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
14 Higher-order functions e.g. (mapcar #'+ '(2 4 6 8) '(10 20 30 40)) (mapcar #'factorial '(2 4 6 8 10) ) e.g. (mapcar #'+ '(2 4 6 8) '(10 20 30 40)) (mapcar #'factorial '(2 4 6 8 10) ) 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 #'+ '(2 4 6 8)) 20 (apply #'append '((2 4 6 8)(a b c d))) (2 4 6 8 A B C D) e.g. (apply #'+ '(2 4 6 8)) 20 (apply #'append '((2 4 6 8)(a b c d))) (2 4 6 8 A B C D) Notes: Both + and #'append are two built-in functions. apply is also a built-in higher-order function.
15
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 10 100)) ((1 2) (10 20) (100 200)) (mappend #'self-and-double '(1 10 100)) (1 2 10 20 100 200) Can you define a higher-order function and execute it?
16
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
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 '(1 10 100)) (1 1 10 100 100 10000) (mapcar #'sqr '(1 10 100)) ((1 1) (10 100) (100 10000)) Solution to Ex2.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.