INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Size: px
Start display at page:

Download "INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp"

Transcription

1 INF4820: Algorithms for Artificial Intelligence and Natural Language Processing More Common Lisp Stephan Oepen & Murhaf Fares Language Technology Group (LTG) September 6, 2017

2 Agenda 2 Previously Common Lisp essentials S-expressions (= atoms or lists of s-expressions) Recursion Quote List processing Identity vs. Equality

3 Agenda 2 Previously Common Lisp essentials S-expressions (= atoms or lists of s-expressions) Recursion Quote List processing Identity vs. Equality Today More Common Lisp Higher-order functions Argument lists Iteration: (the mighty) loop Additional data structures

4 Conditional Evaluation 3 Examples? (defparameter foo 42)? (if (numberp foo) "number" "something else")

5 Conditional Evaluation 3 Examples? (defparameter foo 42)? (if (numberp foo) "number" "something else") "number"

6 Conditional Evaluation 3 Examples? (defparameter foo 42)? (if (numberp foo) "number" "something else") "number"? (cond ((< foo 3) "less") ((> foo 3) "more") (t "equal"))

7 Conditional Evaluation 3 Examples? (defparameter foo 42)? (if (numberp foo) "number" "something else") "number"? (cond ((< foo 3) "less") ((> foo 3) "more") (t "equal")) "more"

8 Conditional Evaluation 3 Examples? (defparameter foo 42)? (if (numberp foo) "number" "something else") "number"? (cond ((< foo 3) "less") ((> foo 3) "more") (t "equal")) "more" General Form (if predicate then clause else clause ) (cond ( predicate 1 clause 1 + ) ( predicate 2 clause 2 + ) ( predicate i clause i + ) (t default clause + ))

9 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))

10 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42

11 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42? (foo foo)

12 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42? (foo foo) 42000

13 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42? (foo foo) 42000? foo 42

14 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42? (foo foo) 42000? foo 42? # foo #<Interpreted Function FOO>

15 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42? (foo foo) 42000? foo 42? # foo #<Interpreted Function FOO>? (funcall # foo foo) 42000

16 Rewind: A Note on Symbol Semantics 4 Symbols can have values as functions and variables at the same time. # (sharp-quote) gives us the function object bound to a symbol.? (defun foo (x) (* x 1000))? (defparameter foo 42) 42? (foo foo) 42000? foo 42? # foo #<Interpreted Function FOO>? (funcall # foo foo) # and funcall (as well as apply) are useful when passing around functions as arguments.

17 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.

18 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))

19 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))

20 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))

21 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp)

22 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp) (22 44)

23 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp) (22 44) Functions

24 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp) (22 44) Functions, recursion

25 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp) (22 44) Functions, recursion, conditionals

26 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp) (22 44) Functions, recursion, conditionals, predicates

27 Higher-Order Functions 5 Functions that accept functions as arguments or return values. Functions in Lisp are first-class objects. Can be created at run-time, passed as arguments, returned as values, stored in variables... just like any other type of data.? (defun filter (list test) (cond ((null list) nil) ((funcall test (first list)) (cons (first list) (filter (rest list) test))) (t (filter (rest list) test))))? (defparameter foo ( ))? (filter foo # evenp) (22 44) Functions, recursion, conditionals, predicates, lists for code and data.

28 Anonymous Functions 6 We can also pass function arguments without first binding them to a name, using lambda expressions: (lambda (parameters) body) A function definition without the defun and symbol part.? (filter foo # (lambda (x) (and (> x 20) (< x 50)))) ( )

29 Anonymous Functions 6 We can also pass function arguments without first binding them to a name, using lambda expressions: (lambda (parameters) body) A function definition without the defun and symbol part.? (filter foo # (lambda (x) (and (> x 20) (< x 50)))) ( ) Typically used for ad-hoc functions that are only locally relevant and simple enough to be expressed inline.

30 Anonymous Functions 6 We can also pass function arguments without first binding them to a name, using lambda expressions: (lambda (parameters) body) A function definition without the defun and symbol part.? (filter foo # (lambda (x) (and (> x 20) (< x 50)))) ( ) Typically used for ad-hoc functions that are only locally relevant and simple enough to be expressed inline. Or, when constructing functions as return values.

31 Returning Functions 7 We have seen how to create anonymous functions using lambda and pass them as arguments. So we can combine that with a function that itself returns another function (which we then bind to a variable).

32 Returning Functions 7 We have seen how to create anonymous functions using lambda and pass them as arguments. So we can combine that with a function that itself returns another function (which we then bind to a variable).? (defparameter foo ( ))? (defun make-range-test (lower upper) # (lambda (x) (and (> x lower) (< x upper))))

33 Returning Functions 7 We have seen how to create anonymous functions using lambda and pass them as arguments. So we can combine that with a function that itself returns another function (which we then bind to a variable).? (defparameter foo ( ))? (defun make-range-test (lower upper) # (lambda (x) (and (> x lower) (< x upper))))? (filter foo (make-range-test 10 30)) (11 22)

34 Parameter Lists: Variable Arities and Naming 8 Optional Parameters? (defun foo (x &optional y (z 42)) (list x y z))? (foo 1) (1 nil 42)? (foo 1 2 3) (1 2 3)

35 Parameter Lists: Variable Arities and Naming 8 Optional Parameters? (defun foo (x &optional y (z 42)) (list x y z)) Keyword Parameters? (defun foo (x &key y (z 42)) (list x y z))? (foo 1) (1 nil 42)? (foo 1 2 3) (1 2 3)? (foo 1) (1 nil 42)? (foo 1 :z 3 :y 2) (1 2 3)

36 Parameter Lists: Variable Arities and Naming 8 Optional Parameters? (defun foo (x &optional y (z 42)) (list x y z)) Keyword Parameters? (defun foo (x &key y (z 42)) (list x y z))? (foo 1) (1 nil 42)? (foo 1 2 3) (1 2 3)? (foo 1) (1 nil 42)? (foo 1 :z 3 :y 2) (1 2 3) Rest Parameters? (defun avg (x &rest rest) (let ((numbers (cons x rest))) (/ (apply # + numbers) (length numbers))))? (avg 3) 3? (avg ) 4

37 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence equalp is like equal but insensitive to case and numeric type.

38 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence equalp is like equal but insensitive to case and numeric type.? (eq (list 1 2 3) (1 2 3)) nil

39 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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

40 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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]

41 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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

42 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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 ) nil

43 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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 ) nil? (equalp ) t

44 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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 ) nil? (equalp ) t? (equal "foo" "foo") t

45 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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 ) nil? (equalp ) t? (equal "foo" "foo") t? (equalp "FOO" "foo") t

46 Recap: Equality for One and All 9 eq tests object identity; it is not useful for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence 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 ) nil? (equalp ) t? (equal "foo" "foo") t? (equalp "FOO" "foo") t Also many type-specialized tests like =, string=, etc.

47 Suggested Home Exercise 10 From the 2013 Final Exam Write two versions of a function swap; one based on recursion and one based on iteration. The function should take three parameters x, y and list where the goal is to replace every element matching x with y in the list list. Here is an example of the expected behavior:? (swap "foo" "bar" ("zap" "foo" "foo" "zap" "foo")) ("zap" "bar" "bar" "zap" "bar") Try to avoid using destructive operations if you can. [7 points]

48 A Brief Detour: Macros 11 Elevator Pitch: programs that generate programs. Macros provide a way for our code to manipulate itself (before it is passed to the compiler). Can implement transformations that extend the syntax of the language. Allows us to control (or even prevent) the evaluation of arguments. We have already encountered some built-in Common Lisp macros: and, or, if, cond, defun, setf, etc.

49 A Brief Detour: Macros 11 Elevator Pitch: programs that generate programs. Macros provide a way for our code to manipulate itself (before it is passed to the compiler). Can implement transformations that extend the syntax of the language. Allows us to control (or even prevent) the evaluation of arguments. We have already encountered some built-in Common Lisp macros: and, or, if, cond, defun, setf, etc. Although macro writing is out of the scope of this course, we will look at perhaps the best example of how macros can redefine the syntax of the language for good or for worse, depending on who you ask: loop

50 Iteration 12 While recursion is a powerful control structure, (let ((result nil)) (dolist (x ( )) (when (evenp x) (push x result))) (reverse result)) (0 2 4) sometimes iteration comes more natural. dolist and dotimes are fine for simple iteration.

51 Iteration 12 While recursion is a powerful control structure, sometimes iteration comes more natural. dolist and dotimes are fine for simple iteration. (let ((result nil)) (dolist (x ( )) (when (evenp x) (push x result))) (reverse result)) (0 2 4) (let ((result nil)) (dotimes (x 6) (when (evenp x) (push x result))) (reverse result)) (0 2 4)

52 Iteration While recursion is a powerful control structure, sometimes iteration comes more natural. dolist and dotimes are fine for simple iteration. But (the mighty) loop is much more general and versatile. (let ((result nil)) (dolist (x ( )) (when (evenp x) (push x result))) (reverse result)) (0 2 4) (let ((result nil)) (dotimes (x 6) (when (evenp x) (push x result))) (reverse result)) (0 2 4) (loop for x below 6 when (evenp x) collect x) (0 2 4) 12

53 Iteration with loop 13 (loop for i from 10 to 50 by 10 collect i) ( ) Illustrates the power of syntax extension through macros; loop is basically a mini-language for iteration.

54 Iteration with loop 13 (loop for i from 10 to 50 by 10 collect i) ( ) Illustrates the power of syntax extension through macros; loop is basically a mini-language for iteration. Reduced uniformity: different syntax based on special keywords. Paul Graham on loop: one of the worst flaws in Common Lisp.

55 Iteration with loop 13 (loop for i from 10 to 50 by 10 collect i) ( ) Illustrates the power of syntax extension through macros; loop is basically a mini-language for iteration. Reduced uniformity: different syntax based on special keywords. Paul Graham on loop: one of the worst flaws in Common Lisp. But non-lispy as it may be, loop is extremely general and powerful!

56 loop: A Few More Examples 14? (loop for i below 10 when (oddp i) sum i) 25

57 loop: A Few More Examples 14? (loop for i below 10 when (oddp i) sum i) 25? (loop for x across "foo" collect x) (#\f #\o #\o)

58 loop: A Few More Examples 14? (loop for i below 10 when (oddp i) sum i) 25? (loop for x across "foo" collect x) (#\f #\o #\o)? (loop with foo = (a b c d) for i in foo for j from 0 until (eq i c) do (format t "~a: ~a ~%" j i)) 0: A 1: B

59 loop: Even More Examples 15? (loop for i below 10 if (evenp i) collect i into evens else collect i into odds finally (return (list evens odds))) (( ) ( ))

60 loop: Even More Examples 15? (loop for i below 10 if (evenp i) collect i into evens else collect i into odds finally (return (list evens odds))) (( ) ( ))? (loop for value being each hash-value of *dictionary* using (hash-key key) do (format t "~&~a -> ~a" key value))

61 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list

62 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ]

63 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table

64 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp

65 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp Accumulation: { collect append sum minimize count... } sexp

66 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp Accumulation: { collect append sum minimize count... } sexp Control: { while until repeat when unless... } sexp

67 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp Accumulation: { collect append sum minimize count... } sexp Control: { while until repeat when unless... } sexp Local variables: with symbol = sexp

68 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp Accumulation: { collect append sum minimize count... } sexp Control: { while until repeat when unless... } sexp Local variables: with symbol = sexp Initialization and finalization: { initially finally } sexp +

69 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp Accumulation: { collect append sum minimize count... } sexp Control: { while until repeat when unless... } sexp Local variables: with symbol = sexp Initialization and finalization: { initially finally } sexp + All of these can be combined freely, e.g. iterating through a list, counting a range, and stepwise computation, all in parallel.

70 loop: The Swiss Army Knife of Iteration 16 Iteration over lists or vectors: for symbol { in on across } list Counting through ranges: for symbol [ from number ] { to downto } number [ by number ] Iteration over hash tables: for symbol being each { hash-key hash-value } in hash table Stepwise computation: for symbol = sexp then sexp Accumulation: { collect append sum minimize count... } sexp Control: { while until repeat when unless... } sexp Local variables: with symbol = sexp Initialization and finalization: { initially finally } sexp + All of these can be combined freely, e.g. iterating through a list, counting a range, and stepwise computation, all in parallel. Note: without at least one accumulator, loop will only return nil.

71 Input and Output 17 Reading and writing is mediated through streams. The symbol t indicates the default stream, the terminal.? (format t "~a is the ~a.~%" 42 "answer") 42 is the answer. nil

72 Input and Output 17 Reading and writing is mediated through streams. The symbol t indicates the default stream, the terminal.? (format t "~a is the ~a.~%" 42 "answer") 42 is the answer. nil (read-line stream nil) reads one line of text from stream, returning it as a string. (read stream nil) reads one well-formed s-expression. The second reader argument asks to return nil on end-of-file.

73 Input and Output 17 Reading and writing is mediated through streams. The symbol t indicates the default stream, the terminal.? (format t "~a is the ~a.~%" 42 "answer") 42 is the answer. nil (read-line stream nil) reads one line of text from stream, returning it as a string. (read stream nil) reads one well-formed s-expression. The second reader argument asks to return nil on end-of-file. (with-open-file (stream "sample.txt" :direction :input) (loop for line = (read-line stream nil) while line do (format t "~a~%" line)))

74 More Data Structures: Arrays 18 Integer-indexed container (indices count from zero)? (setf array (make-array 5)) #(nil nil nil nil nil)? (setf (aref array 0) 42) 42? array #(42 nil nil nil nil)

75 More Data Structures: Arrays 18 Integer-indexed container (indices count from zero)? (setf array (make-array 5)) #(nil nil nil nil nil)? (setf (aref array 0) 42) 42? array #(42 nil nil nil nil) Can be fixed-sized (default) or dynamically adjustable. Can also represent grids of multiple dimensions:? (defparameter array (make-array (2 5) :initial-element 0)) #(( ) ( ))? (incf (aref array 1 2))

76 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:

77 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3

78 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f

79 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b)))

80 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2

81 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"

82 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"? (substitute #\a #\o "hoho") "haha"

83 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"? (substitute #\a #\o "hoho") "haha"? (remove a (a b b a)) (b b)

84 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"? (substitute #\a #\o "hoho") "haha"? (remove a (a b b a)) (b b)? (some # listp (1 a "2" 3 (b)))

85 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"? (substitute #\a #\o "hoho") "haha"? (remove a (a b b a)) (b b)? (some # listp (1 a "2" 3 (b))) T

86 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"? (substitute #\a #\o "hoho") "haha"? (remove a (a b b a)) (b b)? (some # listp (1 a "2" 3 (b)))? (sort ( ) # <) ( )

87 Arrays: Specializations and Generalizations 19 Vectors specialized type of arrays: one-dimensional. Strings specialized type of vectors (similarly: bit vectors). Vectors and lists are subtypes of the abstract data type sequence. Large number of built-in sequence functions, e.g.:? (length "foo") 3? (elt "foo" 0) #\f? (count-if # numberp (1 a "2" 3 (b))) 2? (subseq "foobar" 3 6) "bar"? (substitute #\a #\o "hoho") "haha"? (remove a (a b b a)) (b b)? (some # listp (1 a "2" 3 (b)))? (sort ( ) # <) ( ) Others: position, every, count, remove-if, find, merge, map, reverse, concatenate, reduce,...

88 In Conclusion 20

89 More Practically: Programming in INF In the IFI Linux environment, we have available Allegro Common Lisp, a commercial Lisp interpreter and compiler. We provide a pre-configured, integrated setup with emacs and the SLIME Lisp interaction mode. First-time users, please spend some time studying basic keyboard commands, for example: C-h t and M-x doctor RET. Several open-source Lisp implementation exist, e.g. Clozure or SBCL, compatible with SLIME, so feel free to experiment (on your own time). We have posted a Getting Started guide and Emacs Cheat Sheet on the course pages last week.

90 Good Lisp Style 22 Bottom-Up Design Instead of trying to solve everything with one large function: Build your program with layers of smaller functions. Eliminate repetition and patterns. Related; define abstraction barriers. Separate the code that uses a given data abstraction from the code that implement that data abstraction. Promotes code re-use: Makes the code shorter and easier to read, debug and maintain.

91 Good Lisp Style 22 Bottom-Up Design Instead of trying to solve everything with one large function: Build your program with layers of smaller functions. Eliminate repetition and patterns. Related; define abstraction barriers. Separate the code that uses a given data abstraction from the code that implement that data abstraction. Promotes code re-use: Makes the code shorter and easier to read, debug and maintain. Somewhat more mundane: Adhere to the time-honored 80 column rule. Close multiple parens on the same line. Use auto-indentation (TAB) in Emacs.

92 If you can t see the forest for the trees or can t even see the trees for the parentheses. A Lisp specialty: Uniformity Lisp beginners can sometimes find the syntax overwhelming. What s with all the parentheses? For seasoned Lispers the beauty lies in the fact that there s hardly any syntax at all (beyond the abstract data type of lists). Lisp code is a Lisp data structure. Lisp programs are trees of sexps (comparable to the abstract syntax trees created internally by the parser/compiler for other languages). Makes it easier to write code that generates code: macros.

93 Next week 24 Can we automatically infer the meaning of words? Distributional semantics Vector spaces: Spatial models for representing data Semantic spaces

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals 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

More information

A little bit of Lisp

A little bit of Lisp B.Y. Choueiry 1 Instructor s notes #3 A little bit of Lisp Introduction to Artificial Intelligence CSCE 476-876, Fall 2017 www.cse.unl.edu/~choueiry/f17-476-876 Read LWH: Chapters 1, 2, 3, and 4. Every

More information

INF4820. Common Lisp: Closures and Macros

INF4820. Common Lisp: Closures and Macros INF4820 Common Lisp: Closures and Macros Erik Velldal University of Oslo Oct. 19, 2010 Erik Velldal INF4820 1 / 22 Topics for Today More Common Lisp A quick reminder: Scope, binding and shadowing Closures

More information

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89 Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

More information

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides CS 480 Lisp J. Kosecka George Mason University Lisp Slides Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic

More information

Common LISP Tutorial 1 (Basic)

Common LISP Tutorial 1 (Basic) Common LISP Tutorial 1 (Basic) CLISP Download https://sourceforge.net/projects/clisp/ IPPL Course Materials (UST sir only) Download https://silp.iiita.ac.in/wordpress/?page_id=494 Introduction Lisp (1958)

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. LISP Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits. From one perspective, sequences of bits can be interpreted as a code for ordinary decimal digits,

More information

LISP - SEQUENCES. The function make-sequence allows you to create a sequence of any type. The syntax for this function is:

LISP - SEQUENCES. The function make-sequence allows you to create a sequence of any type. The syntax for this function is: http://www.tutorialspoint.com/lisp/lisp_sequences.htm LISP - SEQUENCES Copyright tutorialspoint.com Sequence is an abstract data type in LISP. Vectors and lists are the two concrete subtypes of this data

More information

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial About this Document This document was written to accompany an in-person Lisp tutorial. Therefore, the information on this document alone is not likely to be sufficient

More information

Artificial Intelligence Programming

Artificial Intelligence Programming Artificial Intelligence Programming Rob St. Amant Department of Computer Science North Carolina State University Lisp basics NC State University 2 / 99 Why Lisp? Some recent Lisp success stories include

More information

Department of Computer and information Science Norwegian University of Science and Technology

Department of Computer and information Science Norwegian University of Science and Technology Department of Computer and information Science Norwegian University of Science and Technology http://www.idi.ntnu.no/ A Crash Course in LISP MNFIT272 2002 Anders Kofod-Petersen anderpe@idi.ntnu.no Introduction

More information

Lecture #2 Kenneth W. Flynn RPI CS

Lecture #2 Kenneth W. Flynn RPI CS Outline Programming in Lisp Lecture #2 Kenneth W. Flynn RPI CS Items from last time Recursion, briefly How to run Lisp I/O, Variables and other miscellany Lists Arrays Other data structures Jin Li lij3@rpi.edu

More information

Common LISP-Introduction

Common LISP-Introduction Common LISP-Introduction 1. The primary data structure in LISP is called the s-expression (symbolic expression). There are two basic types of s-expressions: atoms and lists. 2. The LISP language is normally

More information

Common Lisp. Blake McBride

Common Lisp. Blake McBride Contents Common Lisp Blake McBride (blake@mcbride.name) 1 Data Types 2 2 Numeric Hierarchy 3 3 Comments 3 4 List Operations 4 5 Evaluation and Quotes 5 6 String Operations 5 7 Predicates 6 8 Math Predicates

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages. Lecture LISP Programming Language An Introduction Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

More information

Lecture Notes on Lisp A Brief Introduction

Lecture Notes on Lisp A Brief Introduction Why Lisp? Lecture Notes on Lisp A Brief Introduction Because it s the most widely used AI programming language Because Prof Peng likes using it Because it s good for writing production software (Graham

More information

Imperative, OO and Functional Languages A C program is

Imperative, OO and Functional Languages A C program is Imperative, OO and Functional Languages A C program is a web of assignment statements, interconnected by control constructs which describe the time sequence in which they are to be executed. In Java programming,

More information

CSCI337 Organisation of Programming Languages LISP

CSCI337 Organisation of Programming Languages LISP Organisation of Programming Languages LISP Getting Started Starting Common Lisp $ clisp i i i i i i i ooooo o ooooooo ooooo ooooo I I I I I I I 8 8 8 8 8 o 8 8 I \ `+' / I 8 8 8 8 8 8 \ `-+-' / 8 8 8 ooooo

More information

Symbolic Computation and Common Lisp

Symbolic Computation and Common Lisp Symbolic Computation and Common Lisp Dr. Neil T. Dantam CSCI-56, Colorado School of Mines Fall 28 Dantam (Mines CSCI-56) Lisp Fall 28 / 92 Why? Symbolic Computing: Much of this course deals with processing

More information

Introduction to Lisp

Introduction to Lisp Last update: February 16, 2010 Introduction to Lisp Dana Nau Dana Nau 1 Outline I assume you know enough about computer languages that you can learn new ones quickly, so I ll go pretty fast If I go too

More information

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY, FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší

More information

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp. Overview Announcement Announcement Lisp Basics CMUCL to be available on sun.cs. You may use GNU Common List (GCL http://www.gnu.org/software/gcl/ which is available on most Linux platforms. There is also

More information

Algorithms for AI and NLP (INF4820 Lisp & FSAs)

Algorithms for AI and NLP (INF4820 Lisp & FSAs) S NP Det N VP V The dog barked LTOP h 1 INDEX e 2 def q rel bark v rel prpstn m rel LBL h 4 dog n rel LBL h RELS LBL h 1 ARG0 x 5 LBL h 9 8 ARG0 e MARG h 3 RSTR h 6 ARG0 x 2 5 ARG1 x BODY h 5 7 HCONS h

More information

Robot Programming with Lisp

Robot Programming with Lisp 2. Imperative Programming Institute for Artificial University of Bremen Lisp the Language LISP LISt Processing language 2 Lisp the Language LISP LISt Processing language (LISP Lots of Irritating Superfluous

More information

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti L I S P - Introduction L I S P

More information

Functions, Conditionals & Predicates

Functions, Conditionals & Predicates Functions, Conditionals & Predicates York University Department of Computer Science and Engineering 1 Overview Functions as lambda terms Defining functions Variables (bound vs. free, local vs. global)

More information

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

More information

(defvar *state* nil "The current state: a list of conditions.")

(defvar *state* nil The current state: a list of conditions.) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;; GPS engine for blocks world ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defvar *dbg-ids* nil "Identifiers used by dbg") (defvar *state* nil "The current state: a list of conditions.")

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

Allegro CL Certification Program

Allegro CL Certification Program Allegro CL Certification Program Lisp Programming Series Level I Review David Margolies 1 Summary 1 A lisp session contains a large number of objects which is typically increased by user-created lisp objects

More information

Recursion & Iteration

Recursion & Iteration Recursion & Iteration York University Department of Computer Science and Engineering 1 Overview Recursion Examples Iteration Examples Iteration vs. Recursion Example [ref.: Chap 5,6 Wilensky] 2 Recursion

More information

Lisp. Versions of LISP

Lisp. Versions of LISP Lisp Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks is based on Common Lisp Scheme is one of the major

More information

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Notes on Higher Order Programming in Scheme. by Alexander Stepanov by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

More information

Lisp Basic Example Test Questions

Lisp Basic Example Test Questions 2009 November 30 Lisp Basic Example Test Questions 1. Assume the following forms have been typed into the interpreter and evaluated in the given sequence. ( defun a ( y ) ( reverse y ) ) ( setq a (1 2

More information

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires 1 A couple of Functions 1 Let's take another example of a simple lisp function { one that does insertion sort. Let us assume that this sort function takes as input a list of numbers and sorts them in ascending

More information

Documentation for LISP in BASIC

Documentation for LISP in BASIC Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003 CS 314, LS, LTM: Functional Programming 1 Scheme A program is an expression to be evaluated (in

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

More information

Type-checking on Heterogeneous Sequences

Type-checking on Heterogeneous Sequences Type-checking on Heterogeneous Sequences in Common Lisp Jim Newton EPITA/LRDE May 9, 2016 Jim Newton (EPITA/LRDE) Type-checking on Heterogeneous Sequences May 9, 2016 1 / 31 Overview 1 Introduction Common

More information

Robot Programming with Lisp

Robot Programming with Lisp 4. Functional Programming: Higher-order Functions, Map/Reduce, Lexical Scope Institute for Artificial University of Bremen 9 of November, 2017 Functional Programming Pure functional programming concepts

More information

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky. λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky Mathematical theory for anonymous functions» functions that have

More information

Debugging in LISP. trace causes a trace to be printed for a function when it is called

Debugging in LISP. trace causes a trace to be printed for a function when it is called trace causes a trace to be printed for a function when it is called ;;; a function that works like reverse (defun rev (list) (cons (first (last list)) (rev (butlast list)))) USER: (trace rev) ; note trace

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

19 Machine Learning in Lisp

19 Machine Learning in Lisp 19 Machine Learning in Lisp Chapter Objectives Chapter Contents ID3 algorithm and inducing decision trees from lists of examples. A basic Lisp implementation of ID3 Demonstration on a simple credit assessment

More information

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4

CSCE476/876 Fall Homework 3: Programming Assignment Using Emacs and Common Lisp. 1 Exercises (15 Points) 2. 2 Find (6 points) 4 CSCE476/876 Fall 2018 Homework 3: Programming Assignment Using Emacs and Common Lisp Assigned on: Monday, September 10 th, 2018. Due: Monday, September 24 th, 2018. Contents 1 Eercises (15 Points) 2 2

More information

Streams and Lazy Evaluation in Lisp

Streams and Lazy Evaluation in Lisp Streams and Lazy Evaluation in Lisp Overview Different models of expression evaluation Lazy vs. eager evaluation Normal vs. applicative order evaluation Computing with streams in Lisp Motivation Unix Pipes

More information

A LISP Interpreter in ML

A LISP Interpreter in ML UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,

More information

User-defined Functions. Conditional Expressions in Scheme

User-defined Functions. Conditional Expressions in Scheme User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

More information

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example. Racket Next 2+ weeks will use the Racket language (not ML) and the DrRacket programming environment (not emacs) Installation / basic usage instructions on course website CSE34: Programming Languages Lecture

More information

A Genetic Algorithm Implementation

A Genetic Algorithm Implementation A Genetic Algorithm Implementation Roy M. Turner (rturner@maine.edu) Spring 2017 Contents 1 Introduction 3 2 Header information 3 3 Class definitions 3 3.1 Individual..........................................

More information

Introductory Scheme. Revision 1

Introductory Scheme. Revision 1 Introductory Scheme Revision 1 Joseph W. Lavinus and James D. Arthur (lavinus@cs.vt.edu and arthur@cs.vt.edu) Department of Computer Science Virginia Polytechnic Institute and State University Blacksburg,

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

Allegro CL Certification Program

Allegro CL Certification Program Allegro CL Certification Program Lisp Programming Series Level I Presented by 1 About David Margolies Manager, Documentation, Franz Inc Been working with Lisp since 1984 dm@franz.com 2 About Franz Inc.

More information

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

XLISP PLUS. Reference Manual. Version 3.0

XLISP PLUS. Reference Manual. Version 3.0 XLISP PLUS Reference Manual Version 3.0 XLISP-PLUS: Another Object-oriented Lisp Version 3.0 May 12, 2011 Tom Almy tom@almy.us Portions of this manual and software are from XLISP which is Copyright (c)

More information

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Announcements. Today s Menu

Announcements. Today s Menu Announcements 1 Today s Menu Finish Introduction to ANNs (original material by Dr. Mike Nechyba) > Slides 58-60 > Adjusting the Learning Rate Loose Ends in > Lambda Expressions Review > I/O Functions >

More information

FP Foundations, Scheme

FP Foundations, Scheme FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

More information

A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

More information

University of Massachusetts Lowell

University of Massachusetts Lowell University of Massachusetts Lowell 91.301: Organization of Programming Languages Fall 2002 Quiz 1 Solutions to Sample Problems 2 91.301 Problem 1 What will Scheme print in response to the following statements?

More information

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator. LISP NOTES #1 LISP Acronymed from List Processing, or from Lots of Irritating Silly Parentheses ;) It was developed by John MacCarthy and his group in late 1950s. Starting LISP screen shortcut or by command

More information

CSC 533: Programming Languages. Spring 2015

CSC 533: Programming Languages. Spring 2015 CSC 533: Programming Languages Spring 2015 Functional programming LISP & Scheme S-expressions: atoms, lists functional expressions, evaluation, define primitive functions: arithmetic, predicate, symbolic,

More information

Functional Programming

Functional Programming Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

More information

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters. CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

More information

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7 Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

More information

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13 Lisp: Question 1 Write a recursive lisp function that takes a list as an argument and returns the number of atoms on any level of the list. For instance, list (A B (C D E) ()) contains six atoms (A, B,

More information

NST: A Unit Test Framework for Common Lisp

NST: A Unit Test Framework for Common Lisp Smart Information Flow Technologies (SIFT, LLC) TC-lispers, June 9, 2009 Outline 1 Unit testing 2 3 The basic idea Early implementations, and other lessons How it maybe should work 4 What is unit testing?

More information

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7))) Homework 1 Due 13 September Handout 2 CSC 131: Fall, 2006 6 September Reading 1. Read Mitchell, Chapter 3. 2. The Scheme Tutorial and the Scheme Quick Reference from the Links web page, as needed for the

More information

Functional programming techniques

Functional programming techniques Functional programming techniques o Currying o Continuations o Streams. Lazy evaluation Currying o Haskell B. Curry. o Second-order programming: o Functions that return other functions. o Example: A two-arguments

More information

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

CMSC 331 Final Exam Fall 2013

CMSC 331 Final Exam Fall 2013 CMSC 331 Final Exam Fall 2013 Name: UMBC username: You have two hours to complete this closed book exam. Use the backs of these pages if you need more room for your answers. Describe any assumptions you

More information

Object Oriented Programming (OOP)

Object Oriented Programming (OOP) Object Oriented Programming (OOP) o New programming paradigm o Actions Objects o Objects Actions o Object-oriented = Objects + Classes + Inheritance Imperative programming o OOP (Object-Oriented Programming)

More information

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios.

Jatha. Common Lisp in Java. Ola Bini JRuby Core Developer ThoughtWorks Studios. Jatha Common Lisp in Java Ola Bini JRuby Core Developer ThoughtWorks Studios ola.bini@gmail.com http://olabini.com/blog Common Lisp? Common Lisp? ANSI standard Common Lisp? ANSI standard Powerful Common

More information

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

1 CLWEB INTRODUCTION 1

1 CLWEB INTRODUCTION 1 1 CLWEB INTRODUCTION 1 1. Introduction. This is CLWEB, a literate programming system for Common Lisp by Alex Plotnick plotnick@cs.brandeis.edu. It is modeled after the CWEB system by Silvio Levy and Donald

More information

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan

CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan CS 342 Lecture 7 Syntax Abstraction By: Hridesh Rajan 1 Reading SICP, page 11-19, Section 1.1.6 Little Schemer, Chapter 2 2 The Idea of Syntax Abstraction Problem. Often programming tasks are repetitive,

More information

Intro. Scheme Basics. scm> 5 5. scm>

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Lecture #5 Kenneth W. Flynn RPI CS

Lecture #5 Kenneth W. Flynn RPI CS Outline Programming in Lisp Lecture #5 Kenneth W. Flynn RPI CS We've seen symbols in three contexts so far: > (setf sym ) (let ((sym ))...) >'Sym SYM -- Context The first of these refers to a special (or

More information

CS 842 Ben Cassell University of Waterloo

CS 842 Ben Cassell University of Waterloo CS 842 Ben Cassell University of Waterloo Recursive Descent Re-Cap Top-down parser. Works down parse tree using the formal grammar. Built from mutually recursive procedures. Typically these procedures

More information

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Lisp as a Second Language Chapter 1 Functional Style

Lisp as a Second Language Chapter 1 Functional Style 1 of 61 05/07/2018, 11:35 [an error occurred while processing this directive] Lisp as a Second Language Chapter 1 Functional Style Draft mars 11, 1997 1. Lists as the main data structure 2. Abstraction

More information

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University Introduction to ACL2 CS 680 Formal Methods for Computer Verification Jeremy Johnson Drexel University ACL2 www.cs.utexas.edu/~moore/acl2 ACL2 is a programming language, logic, and theorem prover/checker

More information

University of Maine School of Computing and Information Science COS 470/570

University of Maine School of Computing and Information Science COS 470/570 University of Maine School of Computing and Information Science COS 470/570 Assignment 1: Lisp and Robot Familiarization Assigned 8/28 Due 9/13 This assignment is meant to familiarize you with the tools

More information

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE, FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti Comments in Lisp ; comments

More information

Project 2: Scheme Interpreter

Project 2: Scheme Interpreter Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

More information

UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

More information

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions 1 CSCE 314: Programming Languages Dr. Flemming Andersen Haskell Functions 2 Outline Defining Functions List Comprehensions Recursion 3 Conditional Expressions As in most programming languages, functions

More information

Introduction to Scheme

Introduction to Scheme How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

More information

Functional Languages. Hwansoo Han

Functional Languages. Hwansoo Han Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Copyright 2009 Addison-Wesley. All rights reserved. 1-2 Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

More information