Functional programming with Common Lisp

Size: px
Start display at page:

Transcription

1 Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, / 81 Expressions and functions Expressions are written as lists, using prefix notation. Prefix notation is a form of notation for logic, arithmetic, and algebra. It places operators to the left of their operands. For example, the (infix) expression 14 (2 3) is written as ( 14 ( 2 3)). The first element in an expression list is the name of a function and the remainder of the list are the arguments: (function name arguments) 2 / 81

2 Arity of functions The term arity is used to describe the number of arguments or operands that a function takes. A unary function (arity 1) takes one argument. A binary function (arity 2) takes two arguments. A ternary function (arity 3) takes three arguments, and an n-ary function takes n arguments. Variable arity functions can take any number of arguments. ( ) ; Equivalent to infix ( ). ; Returns 10. (* 2 3 4) ; Equivalent to infix (2 * 3 * 4). Returns 24. (< 1 3 2) ; Equivalent to (1 < 3 < 2). ; Returns NIL (false). 3 / 81 Prohibiting expression evaluation The subexpressions of a procedure application are evaluated, whereas the subexpressions of a quoted expression are not. (/ (* 2 6) 3) ; Returns 4. (/ (* 2 6) 3) ; Returns (/ (* 2 6) 3). 4 / 81

3 Boolean operations Lisp supports Boolean logic with operators and, or, and not. The two former have variable arity, and the last one is a unary operator. The or Boolean operator evaluates its subexpressions from left to right and stops immediately (without evaluating the remaining expression) if any subexpression evaluates to true. In the example below the or function will return true which is the value of (> x 3). Note that the values true/false are denoted in Lisp by t/nil respectively. > (let ((x 5)) (or (< x 2) (> x 3))) T 5 / 81 Boolean operations /cont. The and Boolean operator evaluates its subexpressions from left to right and stops immediately (without evaluating the remaining expression) if any subexpression evaluates to false. In the example below the and function will return nil which is the value of (< x 3). > (let ((x 5)) (and (< x 7) (< x 3))) NIL Consider another example: >(or (and (= 1 1) (< 5 6)) (not (> 3 1))) T 6 / 81

4 Constructing lists We have three (built-in) functions to create a list which are summarized below: 1. cons: creates a list by adding an element as the head of an existing list. 2. list: creates a list comprised of its arguments. 3. append: creates a list by concatenating existing lists. 7 / 81 Function cons A list in Lisp is singly-linked where each node is a pair of two pointers, the first one pointing to a data element and the second one pointing to the tail of the list with the last node s second pointer pointing to the empty list. > (cons 'a '()) (A) nil or a a > (cons 'a (cons 'b (A B) nil a b > (cons 'a (cons (cons 'b (cons 'c '())) (cons 'd (cons 'e '())))) (A (B C) D E) nil a d e nil b c 8 / 81

5 Function list Lists can be created directly with the list function, which takes any number of arguments, and it returns a list composed of these arguments. (list 1 2 a 3) ; Returns (1 2 A 3). (list 1 (2 3) 4) ; Returns (1 (2 3) 4). (list (+ 2 1) (+ 2 1)) ; Returns ((+ 2 1) 3). (list (list a b 4) 5) ; Returns (1 2 3 (a b 4) 5). 9 / 81 Function append The append function takes any number of list arguments and it returns a list which is the concatenation (join) of its arguments: (append (1 2) (3 4)) ; Returns ( ). (append (1 2 3) () (a) (5 6)) ; Returns (1 2 3 a 5 6). (append (1 2 3 (a b c)) () (d) (4 5)) ; Returns (1 2 3 (QUOTE (a b c)) d 4 5). Note that append expects as its arguments only lists. The following call to append will cause an error since the first argument, 1, is not a list. > (append 1 (4 5 6)) Error: 1 is not of type LIST. 10 / 81

6 Function append for list concatenation To create a list ( ) we must first transform 1 into a list: > (append (list 1) (4 5 6)) ( ) 11 / 81 Accessing a list Only two operations are available. We can only access either the head of a list, or the tail of a list. Operation car (also: first) takes a list as an argument and returns the head of the list. For example, (car (a s d f)) ; Returns a. (car ((a s) d f)) ; Returns (a s). Operation cdr (also: rest) takes a list as an argument and returns the tail of the list. For example, (cdr (a s d f)) ; Returns (s d f). (cdr ((a s) d f)) ; Returns (d f). (cdr ((a s) (d f))) ; Returns ((d f)). 12 / 81

7 Accessing a list /cont. In the following example, we are interested in accessing the second element in a list. The second element is the head of the tail of the list: (car (cdr (1 (3 5) (7 11)))) ; Returns (3 5). 13 / 81 Predicate functions A function whose return value is intended to be interpreted as truth or falsity is called a predicate. The built-in function listp returns true if its argument is a list. For example, (listp (a b c)) ; Returns T (true). (listp 7) ; Returns NIL (false). Other common predicate functions include the following: Predicate (numberp argument) (zerop argument) (evenp argument) (oddp argument) Description Returns true if argument is a number. Returns true if argument is zero. Returns true if argument is an even number. Returns true if argument is an odd number. 14 / 81

8 Control flow: Single selection The simplest single conditional is if. ( if testexpression thenexpression ) ( if testexpression thenexpression elseexpression ) The testexpression is a predicate while the thenexpression and the (optional) elseexpression are expressions. 15 / 81 Control flow: Multiple selection Multiple selection can be formed with a cond expression which contains a list of clauses where each clause contains two expressions, called condition and answer. Optionally, we can have an else. ( cond (question answer) )... (else answer) ; Optional. Conditions are evaluated sequentially. For the first condition that evaluates to true, Lisp evaluates the corresponding answer, and the value of the answer is the value of the entire cond expression. If the last condition is else and all other conditions fail, the answer for the cond expression is the value of the last answer expression. We can also use t (true) in place of else. 16 / 81

9 Variables and binding Binding is a mechanism for implementing lexical scope for variables. The let syntactic form takes two arguments: a list of bindings and an expression (the body of the binding) in which to use these bindings. ( let ( (binding 1 ) (binding 2 )... ) (expression) ) where (binding n ) is of the form (variable n value). 17 / 81 Variables and binding /cont. The let values are computed and bindings are done in parallel, which requires all of the definitions to be independent. In the example below, x and y are let-bound variables; they are only visible within the body of the let. (let ((x 2) (y 3)) (+ x y)) ; Returns / 81

10 Context and nested binding An operator like let creates a new lexical context. Within this context there are new variables, and variables from outer contexts may become invisible. A binding can have different values at the same time: (let ((a 1)) (let ((a 2)) (let ((a 3))...))) Here, variable a has three distinct bindings by the time the body (marked by...) executes in the innermost let. 19 / 81 Context and nested binding /cont. The inner binding for a variable shadows the outer binding and the region where a variable binding is visible is called its scope. Consider the following example: (let ((x 1)) ; x is 1. (let ((x (+ x 1))) ; x is 2. (+ x x))) ; Returns / 81

11 Context and nested binding /cont. What if we want the value of one new variable to depend on the value of another variable established by the same expression? In that case we have to use a variant called let*. A let* is functionally equivalent to a series of nested lets. Consider the following example: (let* ((x 10) (y (* 2 x))) ; Not legal for let. (* x y)) ; Returns / 81 Defining functions We can define new functions using defun. A function definition looks like this: ( defun name ( formal parameter list ) body ) 22 / 81

12 Example: Defining functions Consider function absdiff takes two arguments and returns their absolute difference: (defun absdiff (x y) (if (> x y) (- x y) (- y x))) We can execute the function as follows: > (absdiff 3 5) 2 23 / 81 Higher-order functions Higher-order functions are functions which do at least one of the following: 1. Take one or more functions as their arguments. 2. Return a function. The derivative function in calculus is a common example, since it maps a function to another function, e.g. d dx ( x 2 ) = 2x 24 / 81

13 Example: Higher-order functions As an example, consider function sort which takes as an argument a list, constructed through function list, and the comparison operator greater-than (>) and returns a sorted list. >(sort (list ) # >) ( ) 25 / 81 Common higher-order functions in Lisp mapcar takes as its arguments a function and one or more lists and applies the function to the elements of the list(s) in order. ; Multiplication applies to successive pairs. > (mapcar # * (2 3) (10 10)) (20 30) funcall takes as its arguments a function and a list of arguments (does not require arguments to be packaged as a list), and returns the result of applying the function to the elements of the list. > (funcall # ) ; Equivalent to ( ) / 81

14 Common higher-order functions in Lisp /cont. apply works like funcall, but requires that the last argument is a list. > (apply # (1 3 4)) / 81 Anonymous functions An anonymous function is one that is defined, and possibly called, without being bound to an identifier. Unlike functions defined with defun, anonymous functions are not stored in memory. The general syntax of an anonymous function in Lisp (also called lambda expression) is ( lambda (formal parameter list) (body) ) where body is an expression to be evaluated. 28 / 81

15 Anonymous functions /cont. An anonymous function can be applied in the same way that a named function can, e.g. > ((lambda (x) (* x x)) 3) 9 29 / 81 Anonymous functions: Example Consider a function that takes a list as an argument and returns a new list whose elements are the elements of the initial list multiplied by 2. We can perform the multiplication with an anonymous function, and deploy mapcar to apply the anonymous function to the elements of the list as follows: > (mapcar (lambda (n) (* n 2)) ( )) ( ) 30 / 81

16 Side effects in Common Lisp Common Lisp is not a pure functional language as it allows side effects. 31 / 81 Variables and assignments A variable is global if it is visible everywhere as opposed to a local variable which is visible only within the code block in which it is defined. A global variable is accessible everywhere except in expressions that create a new local variable with the same name. Inside code blocks, local values are always looked for first. If a local value for the variable does not exist, then a global value is sought. If no global value is found then the result is an error. We use setq to assign a global variable and setf to assign both global and local variables. The general format is ( setf place value ) and it is used to assign a new value to a place (variable). 32 / 81

17 Examples: Variables and assignments > (setf x (a b c)) (A B C) > (car x) A > (cdr x) (B C) > (cdr (cdr (cdr x))) NIL > (setf x (append x (d e))) (A B C D E) 33 / 81 Examples: Variables and assignments /cont. Variables are essentially pointers. Function eql will return true if its arguments point to the same object, whereas function equal returns true if its arguments have the same value. 34 / 81

18 Examples: Variables and assignments /cont. > x (A B C D E) > (setf y (a b c d e)) (A B C D E) > (eql x y) NIL > (equal x y) T > (setf z x) (A B C D E) > (eql x z) T > (equal x z) T > (eql y z) NIL > (equal y z) T 35 / 81 Defining recursive functions In problem solving, the deployment of recursion implies that the solution to a problem depends on solutions to smaller instances of the same problem. Recursion refers to the practice of defining an object, such as a function or a set, in terms of itself. Every recursive function consists of: One or more base cases, and One or more recursive cases (also called inductive cases). 36 / 81

19 Defining recursive functions Each recursive case consists of: 1. Splitting the data into smaller pieces (for example, with car and cdr), 2. Handling the pieces with calls to the current method (note that every possible chain of recursive calls must eventually reach a base case), and 3. Combining the results into a single result. 37 / 81 Defining recursive functions /cont. A mathematical function uses only recursion and conditional expressions. A mathematical conditional expression is in the form of a list of pairs, each of which is a guarded expression. Each guarded expression consists of a predicate guard and an expression: functionname(arguments) = expression 1 predicateguard 1,... which implies that the function is evaluated by expression n if predicateguard n is true. 38 / 81

20 Example: f : N lists(n) Suppose we need to define the function f : N lists(n) that accepts an integer argument and returns a list, such that f (n) = n, n 1,..., 0 In this and similar problems, we can transform the definition of f (n) into a computable function using available operations on the underlying structure (list). We can use cons as follows: f (n) = n, n 1,..., 1, 0 = cons(n, n 1,..., 1, 0 ) = cons(n, f (n 1)). 39 / 81 Example: f : N lists(n) /cont. We can therefore define f recursively by f (0) = 0. f (n) = cons(n, f (n 1)), for n > / 81

21 Example: f : N lists(n) /cont. We can visually show how this works with a technique called unfolding the definition (or tracing the algorithm ). We can unfold this definition for f (3) as follows: f (3) = cons(3, f (2)) = cons(3, cons(2, f (1))) = cons(3, cons(2, cons(1, f (0)))) = cons(3, cons(2, cons(1, 0 ))) = cons(3, cons(2, 1, 0 )) = cons(3, 2, 1, 0 ) = 3, 2, 1, / 81 Example: f : N lists(n) /cont. We can now build function bsequence as follows: (defun bsequence (n) (if (= n 0) (cons 0 ()) (cons n (bsequence(- n 1))))) 42 / 81

22 Example: Our own version of append Consider function append2 which takes as its arguments two lists lst1 and lst2 and returns a new list which forms a concatenation of lst1 and lst2. Base case: If lst1 is empty, then return lst2. Recursive case: Return a list containing as its first element the head of lst1 with its tail being the concatenation of the tail of lst1 with lst2. (defun append2 (lst1 lst2) (if (null lst1) lst2 (cons (car lst1) (append2 (cdr lst1) lst2)))) 43 / 81 Example: sum Consider function sum which takes a list lst as its argument and returns the summation of its elements. Base case: If the list is empty, then sum is 0. Recursive case: Add the head element to the sum of the elements of the tail. We can unfold this definition for sum( 2, 4, 5 ) as follows: sum( 2, 4, 5 ) = 2 + sum( 4, 5 ) = sum( 5 ) = sum( ) = = / 81

23 Example: sum /cont. (defun sum (lst) (cond ((null lst) 0) (t (+ (car lst) (sum (cdr lst)))))) 45 / 81 Example: sum /cont. We can trace the execution of (sum ( )) as follows: (sum ( )) = (+ 1 sum ( )) = (+ 1 (+ 2 sum (3 4 5))) = (+ 1 (+ 2 (+ 3 sum (4 5)))) = (+ 1 (+ 2 (+ 3 (+ 4 sum (5))))) = (+ 1 (+ 2 (+ 3 (+ 4 (+ 5 sum ()))))) = (+ 1 (+ 2 (+ 3 (+ 4 (+ 5 0))))) = / 81

24 Example: Finding the last element in a list Consider a function last2 which takes a list lst as its argument and returns the last element in the list. Base case: If the list has one element (its tail is the empty list), then return this element. Recursive case: Return the last element of the tail of the list. (defun last2 (lst) (cond ((null lst) nil) ((null (cdr lst)) (car lst)) (t (last2 (cdr lst))))) 47 / 81 Example: Reversing a list Consider function reverse2 which takes a list as its argument and returns the reversed list. Base case: If the list is empty, then return the empty list. Recursive case: Recur on the tail of the list and the head of the list. (defun reverse2 (lst) (cond ((null lst) ()) (t (append (reverse2 (cdr lst)) (list (car lst)))))) 48 / 81

25 Example: cube-list Consider a function called cube-list, which takes as argument a list of numbers and returns the same list with each element replaced with its cube. (defun cube-list (lst) (cond ((null lst) nil) (t (let ((elt (car lst))) (cons (* elt elt elt) (cube-list (cdr lst))))))) 49 / 81 Example: Interleaving the elements of two lists Consider function interleave which takes two lists lst1 and lst2 as its arguments and returns a new list whose elements correspond to lists lst1 and lst2 interleaved, i.e. the first element is the from lst1, the second is from lst2, the third from lst1, etc. Base cases: 1. If lst1 is empty, then return lst2. 2. If lst2 is empty, then return lst1. Recursive case: Concatenate the head of lst1 with a list containing the concatenation of the head of lst2 with the interleaved tails of lst1 and lst2. 50 / 81

26 Example: Interleaving the elements of two lists /cont. (defun interleave (lst1 lst2) (cond ((null lst1) lst2) ((null lst2) lst1) (t (cons (car lst1) (cons (car lst2) (interleave (cdr lst1) (cdr lst2))))))) 51 / 81 Example: Removing the first occurrence of an element in a list Consider function remove-first-occurrence which takes as arguments a list lst and an element elt, and returns lst with the first occurrence of elt removed. Base cases: 1. If lst is empty, then return the empty list. 2. If the head of lst is the symbol we want to remove then return the tail of lst. Recursive case: Keep the head of lst and recur on the tail of lst. 52 / 81

27 Example: Removing the first occurrence of an element in a list /cont. (defun remove-first-occurrence (lst elt) (cond ((null lst) nil) ((equal (car lst) elt) (cdr lst)) (t (cons (car lst) (remove-first-occurrence (cdr lst) elt))))) 53 / 81 Example: Removing the first occurrence of an element in a list /cont. Let us trace the execution of (remove-first-occurrence (a e b c d e) e): (remove-first-occurrence (a e b c d e) e) = (cons a ((remove-first-occurrence (e b c d e) e)) = (cons a (b c d e)) = (a b c d e) 54 / 81

28 Example: Removing all occurrences of an element in a list Consider function remove-all-occurrences which takes as arguments a list lst and an element elt, and returns lst with all occurrences of elt removed. Base case: If lst is empty, return the empty list. Recursive cases: There are two cases to consider when the list is not empty. 1. When the head of the list is the same as elt, ignore the head of the list and recur on removing elt from the tail of the list. 2. When the head of the list is not the same as elt, keep the head and recur on removing elt from the tail of the list. 55 / 81 Example: Removing all occurrences of an element in a list /cont. (defun remove-all-occurrences (lst elt) (if (null lst) nil (if (equal (car lst) elt) (remove-all-occurrences (cdr lst) elt) (cons (car lst) (remove-all-occurrences (cdr lst) elt))))) 56 / 81

29 Example: Merge two lists Consider function merge2 which takes as its arguments two sorted lists of non-repetitive numbers and returns a merged list with no redundancies. Base cases: 1. If lst1 is empty, then return lst2. 2. If lst2 is empty, then return lst1. Recursive cases: 1. If the head of lst1 equals to the head of lst2 then ignore this element and recur on the tail of lst1 and lst2. 2. If the head of lst1 is less than the head of lst2, then keep this element and recur on the tail of lst1 and lst2. 3. Otherwise keep the head of lst2 and recur on lst1 and the tail of lst2. 57 / 81 Example: Merge two lists /cont. (defun merge2 (lst1 lst2) (cond ((null lst1) lst2) ((null lst2) lst1) ((= (car lst1) (car lst2)) (merge2 (cdr lst1) lst2)) ((< (car lst1) (car lst2)) (cons (car lst1) (merge2 (cdr lst1) lst2))) (t (cons (car lst2) (merge2 lst1 (cdr lst2)))))) 58 / 81

30 Example: The Fibonacci sequence /cont. We define function fibonacci which takes as its argument a nonnegative integer k and returns the k th Fibonacci number F k. (defun fibonacci (k) (if (or (zerop k) (= k 1)) k (+ (fibonacci (- k 1)) (fibonacci (- k 2))))) The program is rather slow. The reason for this is that F k and F k 1 both must compute F k / 81 Some guidelines on defining functions Unless the function is trivial, break the logic into cases (multiple selection) with cond. When handling lists, you would normally adopt a recursive solution. Treat the empty list as a base case. Normally you would operate on the head of a list (accessible with car) and recur on the tail of the list (accessible with cdr). To delete the head of the list, simply recur on the tail of the list. To keep the head of the list as is, use cons to place it as the head of the returning list (whose tail is determined by the recursive call). Use else (or t) to cover remaining (and to protect against forgotten) cases. 60 / 81

31 Example: Determining a subset relation Consider function issubsetp which takes as arguments two lists representing sets, set1 and set2, and returns true if set1 is a subset of set2. Otherwise, it returns false (nil). Base case: If set1 is empty, then return true. Recursive case: If the first element of set1 is a member of set2, then recur on the rest of the elements of set1, otherwise return false (nil). (defun issubsetp (set1 set2) (if (null set1) t (if (member (car set1) set2) (issubsetp (cdr set1) set2) nil))) 61 / 81 Example: Determining set union Consider function setunion which takes as its arguments two lists lst1 and lst2 representing sets and returns the set union. Base cases: 1. If lst1 is empty, then return lst2. 2. If lst2 is empty, then return lst1. Recursive cases: 1. If the head of lst1 is a member of lst2, then ignore this element and recur on the tail of lst1, and lst2. 2. If the head of lst1 is not a member of lst2, return a list which is the concatenation of this element with the union of the tail of lst1 and lst2. 62 / 81

32 Example: Determining set union /cont. (defun setunion (lst1 lst2) (cond ((null lst1) lst2) ((null lst2) lst1) ((member (car lst1) lst2)(setunion (cdr lst1) lst2)) (t (cons (car lst1) (setunion (cdr lst1) lst2))))) We can execute the function as follows: > (setunion (a b c d) (a d)) (B C A D) 63 / 81 Example: Determining set intersection Consider function setintersection which takes as its arguments two lists lst1 and lst2 representing sets, and returns a new list representing a set which forms the intersection of its arguments. Base case: If either list is empty, then return the empty set. Recursive cases: 1. If the head of lst1 is a member of lst2, then keep this element and recur on the tail of lst1 and lst2. 2. If the head of lst1 is not a member of lst2, ignore this element and recur on the tail of lst1 and lst2. 64 / 81

33 Example: Determining set intersection /cont. (defun setintersection (lst1 lst2) (cond ((null lst1) ()) ((null lst2) ()) ((member (car lst1) lst2) (cons (car lst1)(setintersection (cdr lst1) lst2))) (t (setintersection (cdr lst1) lst2)))) We can execute the function as follows: > (setintersection (a b c) ()) NIL > (setintersection (a b c) (a d e)) (A) 65 / 81 Example: Determining set difference Consider function setdifference which takes as its arguments two lists lst1 and lst2 representing sets and returns the set difference. Base case: If lst1 is empty, then return the empty set. If lst2 is empty, then return lst1. Recursive cases: 1. If the head of lst1 is a member of lst2, then ignore this element and recur on the tail of lst1, and lst2. 2. If the head of lst1 is not a member of lst2, keep this element and recur on the tail of lst1 and lst2. 66 / 81

34 Example: Determining set difference /cont. (defun setdifference (lst1 lst2) (cond ((null lst1) ()) ((null lst2) lst1) ((member (car lst1) lst2)(setdifference (cdr lst1) lst2)) (t (cons (car lst1) (setdifference (cdr lst1) lst2))))) We can execute the function as follows: > (setdifference (a b c) (a d e f)) (B C) 67 / 81 Example: Determining set symmetric difference Consider function setsymmetricdifference which takes as its arguments two lists representing sets and returns a list representing their symmetric difference. We can define this function as the difference between the union and the intersection sets, i.e. A B = (A B) \ (A B) (defun setsymmetricdifference (lst1 lst2) (setdifference (union lst1 lst2)(intersection lst1 lst2))) 68 / 81

35 Example: Determining set symmetric difference /cont. Alternatively we can say A B = (A\B) (B\A) (defun setsymmetricdifference2 (lst1 lst2) (union (setdifference lst1 lst2)(setdifference lst2 lst1))) 69 / 81 Example: Determining set symmetric difference /cont. We can now run the function as follows: > (setsymmetricdifference (a b c d e f) (d e f g h)) (H G A B C) > (setsymmetricdifference2 (a b c d e f) (d e f g h)) (H G A B C) > (setsymmetricdifference (a b (cd) e) (e (f h))) ((F H) A B (CD)) > (setsymmetricdifference2 (a b (cd) e) (e (f h))) ((F H) A B (CD)) 70 / 81

36 Example: Transforming a bag to a set Consider function bag-to-set which takes as its argument a list representing a bag and returns the corresponding set. Base case: If the list is empty, then return the empty list. Recursive cases: 1. If the head of the list is a member of the tail of the list, then ignore this element and recur on the tail of the list. 2. If the head of the list is not a member of the tail of the list, keep the head element and recur on the tail of the list. 71 / 81 Example: Transforming a bag to a set /cont. (defun bag-to-set (bag) (cond ((null bag) ()) ((member (car bag) (cdr bag)) (bag-to-set (cdr bag))) (t (cons (car bag) (bag-to-set(cdr bag)))))) 72 / 81

37 Representing trees We can use a list to represent a non-empty tree as atom, l list, r list, where atom is the root of the tree, and l list and r list represent the left and right subtrees respectively. 73 / 81 Example: Binary tree Consider the binary tree below / 81

38 Example: Binary tree - Translating the representation into Lisp We can represent the entire tree as one single list: (40 ; Root. (30 ; Root of left subtree. (25 () ()) (35 () ()) ) (60 ; Root of right subtree. (50 () ()) () ) ) or (40 (30 (25 () ())(35 () ()))(60 (50 () ())())) 75 / 81 Accessing parts of the tree Recall that the entire tree is represented by the list atom, l list, r list. We can obtain the root of the tree by getting the head of the list: > (car (40 (30 (25 () ())(35 () ()))(60 (50 () ())())) / 81

39 Bubble sort Consider the implementation of function bubble-sort which takes as its argument a list, and returns the same list with its elements sorted in ascending order. We first need to build some auxiliary functions, the first one is bubble which performs one iteration, thus placing one element in its proper position. (defun bubble (lst) (cond ((or (null lst) (null (cdr lst))) lst) (( < (car lst) (car (cdr lst))) (cons (car lst) (bubble (cdr lst)))) ( t (cons (car (cdr lst)) (bubble (cons (car lst) (cdr (cdr lst)))))))) 77 / 81 Bubble sort /cont. Another auxiliary function is is-sortedp which returns True or False on whether or not its list argument is sorted. (defun is-sortedp (lst) (cond ((or (null lst) (null (cdr lst))) t) ((< (car lst) (car (cdr lst))) (is-sortedp (cdr lst))) (t nil))) 78 / 81

40 Bubble sort /cont. We can now put everything together and define bubble-sort as follows: (defun bubble-sort (lst) (cond ((or (null lst) (null (cdr lst))) lst) ((is-sortedp lst) lst) (t (bubble-sort (bubble lst))))) 79 / 81 Linear search If x appears in L, then we would like to return its position in the list. (defun search (lst elt pos) (if (equal (car lst) elt) pos (search (cdr lst) elt (+ 1 pos)))) (defun linear-search (lst elt) (search lst elt 1)) 80 / 81

41 Binary search Recall that we can use a list to represent a non-empty tree as atom, l list, r list, where atom is the root of the tree and l list and r list represent the left and right subtrees respectively. (defun binary-search (lst elt) (cond ((null lst) nil) ((= (car lst) elt) t) ((< elt (car lst)) (binary-search (car (cdr lst)) elt)) ((> elt (car lst)) (binary-search (car (cdr (cdr lst))) elt)))) 81 / 81

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

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).

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

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)

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

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,

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

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

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

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,

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

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

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

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,

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

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,

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

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

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

Ordered structures. Dr. C. Constantinides. Department of Computer Science and Software Engineering Concordia University Montreal, Canada

Ordered structures Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada January 10, 2017 1/19 Introduction Ordered structures include tuples,

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

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

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

An introduction to Scheme

An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

Lambda Calculus see notes on Lambda Calculus

Lambda Calculus see notes on Lambda Calculus Shakil M. Khan adapted from Gunnar Gotshalks recap so far: Lisp data structures basic Lisp programming bound/free variables, scope of variables Lisp symbols,

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

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

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

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

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

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

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,

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

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

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 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

1.3. Conditional expressions To express case distinctions like

Introduction Much of the theory developed in the underlying course Logic II can be implemented in a proof assistant. In the present setting this is interesting, since we can then machine extract from a

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

Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus LC-1 l- Calculus History Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of l-calculus is equivalent in power to

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,

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

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

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

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

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

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

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

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

15 Unification and Embedded Languages in Lisp

15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed

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

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

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

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,

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

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

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

Lambda Calculus LC-1

Lambda Calculus LC-1 λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power

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

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

Evaluating Scheme Expressions

Evaluating Scheme Expressions How Scheme evaluates the expressions? (procedure arg 1... arg n ) Find the value of procedure Find the value of arg 1 Find the value of arg n Apply the value of procedure

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

Example Scheme Function: equal

ICOM 4036 Programming Languages Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

MORE SCHEME COMPUTER SCIENCE MENTORS 61A October 30 to November 3, 2017 1 What Would Scheme Print? Solutions begin on the following page. 1. What will Scheme output? Draw box-and-pointer diagrams to help

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

Functional Programming. Contents

Functional Programming Gunnar Gotshalks 2003 September 27 Contents Introduction... 2 Example for Inner Product... 2 Basis Set for Functionals... 3 Example Function Definitions... 3 Lisp Builtin Functionals...

Scheme Quick Reference

Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

Scheme Quick Reference

Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

Introduction to Functional Programming

Introduction to Functional Programming Xiao Jia xjia@cs.sjtu.edu.cn Summer 2013 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell,

More Scheme CS 331. Quiz. 4. What is the length of the list (()()()())? Which element does (car (cdr (x y z))) extract from the list?

More Scheme CS 331 Quiz 1. What is (car ((2) 3 4))? (2) 2. What is (cdr ((2) (3) (4)))? ((3)(4)) 3. What is (cons 2 (2 3 4))? (2 2 3 4) 4. What is the length of the list (()()()())? 4 5. Which element

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

Lambda Calculus as a Programming Language

Cristian Giumale / Lecture Notes 1 Lambda Calculus as a Programming Language The Lambda calculus can be considered as the machine code of a particular computer. Call it the Lambda machine. As in conventional

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

Midterm Examination (Sample Solutions), Cmput 325

Midterm Examination (Sample Solutions, Cmput 325 [15 marks] Consider the Lisp definitions below (defun test (L S (if (null L S (let ((New (add (cdar L S (test (cdr L New (defun add (A S (if (null S (cons

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 18: Functional Programming Zheng (Eddy) Zhang Rutgers University April 9, 2018 Review: Defining Scheme Functions (define ( lambda (

Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions. 1 Calculator. calc> (+ 2 2) 4

CS 61A Interpreters and Tail Calls Fall 2017 Discussion 8: November 1, 2017 Solutions 1 Calculator We are beginning to dive into the realm of interpreting computer programs that is, writing programs that

61A LECTURE 18 SCHEME. Steven Tang and Eric Tzeng July 24, 2013

61A LECTURE 18 SCHEME Steven Tang and Eric Tzeng July 24, 2013 What s happening today? We re learning a new language! After you know one language (Python), learning your second (Scheme) is much faster

Review of Functional Programming

Review of Functional Programming York University Department of Computer Science and Engineering 1 Function and # It is good programming practice to use function instead of quote when quoting functions.

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..........................................

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

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

.00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

Chapter 1 : What is an application of linear linked list data structures? - Quora A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements

COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

YOUR NAME PLEASE: *** SOLUTIONS *** Computer Science 201b SAMPLE Exam 1 SOLUTIONS February 15, 2015 Closed book and closed notes. No electronic devices. Show ALL work you want graded on the test itself.

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004

Principles of Programming Languages Topic: Scope and Memory Professor Louis Steinberg Fall 2004 CS 314, LS,BR,LTM: Scope and Memory 1 Review Functions as first-class objects What can you do with an integer?

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Chapter 1 Summary Comments are indicated by a hash sign # (also known as the pound or number sign). Text to the right of the hash sign is ignored. (But, hash loses its special meaning if it is part of

A Brief Introduction to Scheme (I)

A Brief Introduction to Scheme (I) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Scheme Scheme I p.1/44 Scheme: Feature Set A

LECTURE 17. Expressions and Assignment

LECTURE 17 Expressions and Assignment EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments)

The Typed Racket Guide

The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

CSE 341 Section Handout #6 Cheat Sheet

Cheat Sheet Types numbers: integers (3, 802), reals (3.4), rationals (3/4), complex (2+3.4i) symbols: x, y, hello, r2d2 booleans: #t, #f strings: "hello", "how are you?" lists: (list 3 4 5) (list 98.5