inf4820 algorithms for artificial intelligence and
play

INF4820: Algorithms for Artificial Intelligence and Natural - PowerPoint PPT Presentation

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017 Last Week: What is AI? Since the 1950s: Chatbots,


  1. Creating our own functions ◮ The special form defun associates a function definition with a symbol: General form (defun name ( parameter 1 . . . parameter n ) body ) 10

  2. Creating our own functions ◮ The special form defun associates a function definition with a symbol: General form (defun name ( parameter 1 . . . parameter n ) body ) Example ? (defun average (x y) (/ (+ x y) 2)) 10

  3. Creating our own functions ◮ The special form defun associates a function definition with a symbol: General form (defun name ( parameter 1 . . . parameter n ) body ) Example ? (defun average (x y) (/ (+ x y) 2)) ? (average 10 20) → 15 10

  4. The ‘Hello World!’ of Functional Programming ◮ Classic example: the factorial function. 11

  5. The ‘Hello World!’ of Functional Programming � 1 if n = 0 n ! = n × ( n − 1)! if n > 0 ◮ Classic example: the factorial function. ◮ A recursive procedure; calls itself, directly or indirectly. 11

  6. The ‘Hello World!’ of Functional Programming � 1 if n = 0 n ! = n × ( n − 1)! if n > 0 ◮ Classic example: the factorial function. (defun ! (n) ◮ A recursive procedure; calls itself, (if (= n 0) directly or indirectly. 1 (* n (! (- n 1))))) 11

  7. The ‘Hello World!’ of Functional Programming � 1 if n = 0 n ! = n × ( n − 1)! if n > 0 ◮ Classic example: the factorial function. (defun ! (n) ◮ A recursive procedure; calls itself, (if (= n 0) directly or indirectly. 1 (* n (! (- n 1))))) ◮ May seem circular, but is well-defined as long as there’s a base case terminating the recursion. 11

  8. The ‘Hello World!’ of Functional Programming � 1 if n = 0 n ! = n × ( n − 1)! if n > 0 ◮ Classic example: the factorial function. (defun ! (n) ◮ A recursive procedure; calls itself, (if (= n 0) directly or indirectly. 1 (* n (! (- n 1))))) ◮ May seem circular, but is well-defined as long as there’s a base case terminating the recursion. def fac(n): ◮ For comparison: a non-recursive r = 1 while (n > 0): implementation (in Python). r = r * n n = n - 1 return r 11

  9. A Special Case of Recursion: Tail Recursion ◮ A more efficient way to (defun ! (n) define n ! recursively. (!-aux 1 1 n)) ◮ Use a helper procedure (defun !-aux (r i n) with an accumulator (if (> i n) variable to collect the r product along the way. (!-aux (* i r) (+ i 1) n))) 12

  10. A Special Case of Recursion: Tail Recursion ◮ A more efficient way to (defun ! (n) define n ! recursively. (!-aux 1 1 n)) ◮ Use a helper procedure (defun !-aux (r i n) with an accumulator (if (> i n) variable to collect the r product along the way. (!-aux (* i r) (+ i 1) n))) 12

  11. A Special Case of Recursion: Tail Recursion ◮ A more efficient way to (defun ! (n) define n ! recursively. (!-aux 1 1 n)) ◮ Use a helper procedure (defun !-aux (r i n) with an accumulator (if (> i n) variable to collect the r product along the way. (!-aux (* i r) ◮ The recursive call is in tail (+ i 1) position; n))) ◮ no work remains to be done in the calling function. ◮ Once we reach the base case, the return value is ready. 12

  12. A Special Case of Recursion: Tail Recursion ◮ A more efficient way to (defun ! (n) define n ! recursively. (!-aux 1 1 n)) ◮ Use a helper procedure (defun !-aux (r i n) with an accumulator (if (> i n) variable to collect the r product along the way. (!-aux (* i r) ◮ The recursive call is in tail (+ i 1) position; n))) ◮ no work remains to be done in the calling function. ◮ Once we reach the base case, the return value is ready. ◮ Most CL compilers do tail call optimization (TCO), so that the recursion is executed as an iterative loop. 12

  13. A Special Case of Recursion: Tail Recursion ◮ A more efficient way to (defun ! (n) define n ! recursively. (!-aux 1 1 n)) ◮ Use a helper procedure (defun !-aux (r i n) with an accumulator (if (> i n) variable to collect the r product along the way. (!-aux (* i r) ◮ The recursive call is in tail (+ i 1) position; n))) ◮ no work remains to be done in the calling function. ◮ Once we reach the base case, the return value is ready. ◮ Most CL compilers do tail call optimization (TCO), so that the recursion is executed as an iterative loop. ◮ (The next lecture will cover CL’s built-in loop construct.) 12

  14. Tracing the processes Recursive Tail-Recursive (defun ! (n) (defun ! (n) (if (= n 0) (!-aux 1 1 n)) 1 (defun !-aux (r i n) (* n (! (- n 1))))) (if (> i n) r (!-aux (* r i) (+ i 1) n))) ? (! 7) ⇒ (* 7 (! 6)) ? (! 7) ⇒ (* 7 (* 6 (! 5))) ⇒ (!-aux 1 1 7) ⇒ (* 7 (* 6 (* 5 (! 4)))) ⇒ (!-aux 1 2 7) ⇒ (* 7 (* 6 (* 5 (* 4 (! 3))))) ⇒ (!-aux 2 3 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 (! 2)))))) ⇒ (!-aux 6 4 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 (! 1))))))) ⇒ (!-aux 24 5 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 1)))))) ⇒ (!-aux 120 6 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 2))))) ⇒ (!-aux 720 7 7) ⇒ (* 7 (* 6 (* 5 (* 4 6)))) ⇒ (!-aux 5040 8 7) ⇒ (* 7 (* 6 (* 5 24))) → 5040 ⇒ (* 7 (* 6 120)) ⇒ (* 7 720) → 5040 13

  15. Tracing the processes Recursive Tail-Recursive (defun ! (n) (defun ! (n) (if (= n 0) (!-aux 1 1 n)) 1 (defun !-aux (r i n) (* n (! (- n 1))))) (if (> i n) r (!-aux (* r i) (+ i 1) n))) ? (! 7) ⇒ (* 7 (! 6)) ? (! 7) ⇒ (* 7 (* 6 (! 5))) ⇒ (!-aux 1 1 7) ⇒ (* 7 (* 6 (* 5 (! 4)))) ⇒ (!-aux 1 2 7) ⇒ (* 7 (* 6 (* 5 (* 4 (! 3))))) ⇒ (!-aux 2 3 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 (! 2)))))) ⇒ (!-aux 6 4 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 (! 1))))))) ⇒ (!-aux 24 5 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 1)))))) ⇒ (!-aux 120 6 7) ⇒ (* 7 (* 6 (* 5 (* 4 (* 3 2))))) ⇒ (!-aux 720 7 7) ⇒ (* 7 (* 6 (* 5 (* 4 6)))) ⇒ (!-aux 5040 8 7) ⇒ (* 7 (* 6 (* 5 24))) → 5040 ⇒ (* 7 (* 6 120)) ⇒ (* 7 720) → 5040 13

  16. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. 14

  17. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 14

  18. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi 14

  19. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi 14

  20. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi ? foobar → error; unbound variable 14

  21. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi ? foobar → error; unbound variable ? ’foobar → foobar 14

  22. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi ? foobar → error; unbound variable ? ’foobar → foobar ? (* 2 pi) → 6.283185307179586d0 14

  23. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi ? foobar → error; unbound variable ? ’foobar → foobar ? (* 2 pi) → 6.283185307179586d0 ? ’(* 2 pi) → (* 2 pi) 14

  24. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi ? foobar → error; unbound variable ? ’foobar → foobar ? (* 2 pi) → 6.283185307179586d0 ? ’(* 2 pi) → (* 2 pi) ? () → error; missing procedure 14

  25. The quote Operator ◮ A special form making expressions self-evaluating. ◮ The quote operator (or simply ‘ ’ ’) suppresses evaluation. ? pi → 3.141592653589793d0 ? (quote pi) → pi ? ’pi → pi ? foobar → error; unbound variable ? ’foobar → foobar ? (* 2 pi) → 6.283185307179586d0 ? ’(* 2 pi) → (* 2 pi) ? () → error; missing procedure ? ’() → () 14

  26. Both Code and Data are S-Expressions ◮ We’ve mentioned how sexps are used to represent both data and code. ◮ Note the double role of lists: ◮ Lists are function calls: ? (* 10 (+ 2 3)) → 50 ? (bar 1 2) → error; function bar undefined 15

  27. Both Code and Data are S-Expressions ◮ We’ve mentioned how sexps are used to represent both data and code. ◮ Note the double role of lists: ◮ Lists are function calls: ? (* 10 (+ 2 3)) → 50 ? (bar 1 2) → error; function bar undefined ◮ But, lists can also be data: ? ’(foo bar) → (foo bar) ? (list ’foo ’bar) → (foo bar) 15

  28. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) 16

  29. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → 16

  30. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) 16

  31. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 16

  32. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) 16

  33. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 16

  34. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 16

  35. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → 16

  36. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil 16

  37. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) 16

  38. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) ? (append ’(1 2) ’(3) ’(4 5 6)) → (1 2 3 4 5 6) 16

  39. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) ? (append ’(1 2) ’(3) ’(4 5 6)) → (1 2 3 4 5 6) ? (length ’(1 2 3)) → 3 16

  40. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) ? (append ’(1 2) ’(3) ’(4 5 6)) → (1 2 3 4 5 6) ? (length ’(1 2 3)) → 3 ? (reverse ’(1 2 3)) → (3 2 1) 16

  41. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) ? (append ’(1 2) ’(3) ’(4 5 6)) → (1 2 3 4 5 6) ? (length ’(1 2 3)) → 3 ? (reverse ’(1 2 3)) → (3 2 1) ? (nth 2 ’(1 2 3)) → 3 16

  42. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) ? (append ’(1 2) ’(3) ’(4 5 6)) → (1 2 3 4 5 6) ? (length ’(1 2 3)) → 3 ? (reverse ’(1 2 3)) → (3 2 1) ? (nth 2 ’(1 2 3)) → 3 ? (last ’(1 2 3)) → (3) 16

  43. LISP = LISt Processing ◮ cons builds up new lists; first and rest destructure them. ? (cons 1 (cons 2 (cons 3 nil))) → (1 2 3) ? (cons 0 ’(1 2 3)) → (0 1 2 3) ? (first ’(1 2 3)) → 1 ? (rest ’(1 2 3)) → (2 3) ? (first (rest ’(1 2 3))) → 2 ? (rest (rest (rest ’(1 2 3)))) → nil ◮ Many additional list operations (derivable from the above), e.g. ? (list 1 2 3) → (1 2 3) ? (append ’(1 2) ’(3) ’(4 5 6)) → (1 2 3 4 5 6) ? (length ’(1 2 3)) → 3 ? (reverse ’(1 2 3)) → (3 2 1) ? (nth 2 ’(1 2 3)) → 3 Wait, why not 3 ? ? (last ’(1 2 3)) → (3) 16

  44. Lists are Really Chained ‘cons’ Cells (1 2 3) � ❅ � ❅ � ✠ ❅ ❘ 1 � ❅ � ❅ � ✠ ❅ ❘ 2 � ❅ � ❅ ✠ � ❅ ❘ 3 nil (cons 1 (cons 2 (cons 3 nil))) 17

  45. Lists are Really Chained ‘cons’ Cells (1 2 3) ((1 2) 3) � ❅ � ❅ � ❅ � ✠ ❘ ❅ � ❅ � ✠ ❘ ❅ 1 � ✁ ❅ � ❅ � ✁ ❅ ✠ � ☛ ✁ ❅ ❘ � ❅ ✠ � ❅ ❘ 1 3 nil ❄ 2 � ❅ � ❅ � ❅ ✠ � ❘ ❅ � ❅ � ✠ ❅ ❘ 3 nil 2 nil (cons 1 (cons 2 (cons 3 nil))) (cons (cons 1 (cons 2 nil)) (cons 3 nil)) 17

  46. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 18

  47. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. 18

  48. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. ◮ General form: (setf place value ) ◮ . . . where place can either be a variable named by a symbol or some other storage location: 18

  49. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. ◮ General form: (setf place value ) ◮ . . . where place can either be a variable named by a symbol or some other storage location: ? (setf *foo* (+ *foo* 1)) 18

  50. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. ◮ General form: (setf place value ) ◮ . . . where place can either be a variable named by a symbol or some other storage location: ? (setf *foo* (+ *foo* 1)) ? *foo* → 43 18

  51. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. ◮ General form: (setf place value ) ◮ . . . where place can either be a variable named by a symbol or some other storage location: ? (setf *foo* (+ *foo* 1)) ? *foo* → 43 ? (setf *foo* ’(2 2 3)) 18

  52. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. ◮ General form: (setf place value ) ◮ . . . where place can either be a variable named by a symbol or some other storage location: ? (setf *foo* (+ *foo* 1)) ? *foo* → 43 ? (setf *foo* ’(2 2 3)) ? (setf (first *foo*) 1) 18

  53. Assigning Values: ‘Generalized Variables’ ◮ defparameter declares a ‘global variable’ and assigns a value: ? (defparameter *foo* 42) → *FOO* ? *foo* → 42 ◮ setf provides a uniform way of assigning values to variables. ◮ General form: (setf place value ) ◮ . . . where place can either be a variable named by a symbol or some other storage location: ? (setf *foo* (+ *foo* 1)) ? *foo* → 43 ? (setf *foo* ’(2 2 3)) ? (setf (first *foo*) 1) ? *foo* → (1 2 3) 18

  54. Some Other Macros for Assignment Example Type of x Effect (incf x y) number (setf x (+ x y)) number (incf x) (incf x 1) number (decf x y) (setf x (- x y)) (decf x) number (decf x 1) list (push y x) (setf x (cons y x)) (pop x) list (let ((y (first x))) (setf x (rest x)) y) list (pushnew y x) (if (member y x) x (push y x)) 19

  55. Some Other Macros for Assignment Example Type of x Effect (incf x y) number (setf x (+ x y)) number (incf x) (incf x 1) number (decf x y) (setf x (- x y)) (decf x) number (decf x 1) list (push y x) (setf x (cons y x)) (pop x) list (let ((y (first x))) (setf x (rest x)) y) list (pushnew y x) (if (member y x) x (push y x)) Shall we write our own push and pop ? 19

  56. Some Other Macros for Assignment Example Type of x Effect (incf x y) number (setf x (+ x y)) number (incf x) (incf x 1) number (decf x y) (setf x (- x y)) (decf x) number (decf x 1) list (push y x) (setf x (cons y x)) (pop x) list (let ((y (first x))) (setf x (rest x)) y) list (pushnew y x) (if (member y x) x (push y x)) Shall we write our own push and pop ? Just a second! 19

  57. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 20

  58. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 20

  59. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 ? *bar* → 20

  60. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 ? *bar* → 100 20

  61. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 ? *bar* → 100 ? baz → 20

  62. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 ? *bar* → 100 ? baz → error; unbound variable 20

  63. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 ? *bar* → 100 ? baz → error; unbound variable ◮ Bindings valid only in the body of let . ◮ Previously existing bindings are shadowed within the lexical scope. 20

  64. Local Variables ◮ Sometimes we want to store intermediate results. ◮ let and let* create temporary value bindings for symbols. ? (defparameter *foo* 42) → *FOO* ? (defparameter *bar* 100) → *BAR* ? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) → 50 ? *bar* → 100 ? baz → error; unbound variable ◮ Bindings valid only in the body of let . ◮ Previously existing bindings are shadowed within the lexical scope. ◮ let* is like let but binds sequentially . 20

  65. Predicates ◮ A predicate tests some condition. ◮ Evaluates to a boolean truth value: ◮ nil (the empty list) means false . ◮ Anything non- nil (including t ) means true . ? (listp ’(1 2 3)) → t ? (null (rest ’(1 2 3))) → nil ? (evenp 2) → t ? (defparameter foo 42) ? (or (not (numberp foo)) (and (>= foo 0) (<= foo 42))) → t ◮ Plethora of equality tests: eq , eql , equal , and equalp . 21

  66. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. 22

  67. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil 22

  68. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t 22

  69. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t ? (eq 42 42) → ? [implementation-dependent] 22

  70. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t ? (eq 42 42) → ? [implementation-dependent] ? (eql 42 42) → t 22

  71. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t ? (eq 42 42) → ? [implementation-dependent] ? (eql 42 42) → t ? (eql 42 42.0) → nil 22

  72. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t ? (eq 42 42) → ? [implementation-dependent] ? (eql 42 42) → t ? (eql 42 42.0) → nil ? (equalp 42 42.0) → t 22

  73. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t ? (eq 42 42) → ? [implementation-dependent] ? (eql 42 42) → t ? (eql 42 42.0) → nil ? (equalp 42 42.0) → t ? (equal "foo" "foo") → t 22

  74. Equality for One and All ◮ eq tests object identity; not applicable for numbers or characters. ◮ eql is like eq , but well-defined on numbers and characters. ◮ equal tests structural equivalence (recursively for lists and strings). ◮ equalp is like equal but insensitive to case and numeric type. ? (eq (list 1 2 3) ’(1 2 3)) → nil ? (equal (list 1 2 3) ’(1 2 3)) → t ? (eq 42 42) → ? [implementation-dependent] ? (eql 42 42) → t ? (eql 42 42.0) → nil ? (equalp 42 42.0) → t ? (equal "foo" "foo") → t ? (equalp "FOO" "foo") → t 22

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend