CS 152 Programming Language Paradigms February 12 Class
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www. cs. sjsu. edu/~mak
Scheme Language Elements o All programs and data in Scheme are expressions. n o Atom n n n o Two types of expressions: atoms and lists Lowest level element Constant (number, string, character, boolean) Symbols (identifiers) List n n One or more expressions separated by spaces. All surrounded by a set of parentheses. _ SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 2
Scheme Language Elements o Atoms 42 an integer value 2. 1 a real value alpha a symbol "hello" a string value #a the character ‘a’ #T o the Boolean value true Lists (2. 1 2. 2 2. 3) a list of real numbers (+ 2 3) a list of the symbol + and two integers (* (+ 1 2) (- 3 4)) a list containing two sublists SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 3
Scheme Evaluation Rule o An atomic literal evaluates to itself. n o Example: 42 A non-keyword symbol evaluates to the value to which it is bound. n n The symbol is treated as an identifier, a name that refers to a value, in the role of a variable. The binding is looked up at run time in the current environment. o n An environment is a symbol table that associates symbols with their values. Example: Assume that symbol alpha is bound to the list (1 2 3). Then alpha (1 2 3) SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 4
Scheme Evaluation Rule: Lists o Special form: The first list item is a keyword. n n o Otherwise, the list is a function application. n n o Use a special evaluation rule, depending on the keyword. Example: The define and let special forms. The first item must evaluate to a function. Apply the function to the remaining items in the list. Recursively evaluate each expression in the list. _ SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 5
Scheme Functions o Expressions are lists written in prefix form. n Example: (+ 2 3) o o Apply function + to the values 2 and 3 and return the value 5. Distinguish: n n n The function itself, as an object. A call to the function, written as a list expression. The return value of a function call. SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 6
Function Evaluations o Recursively evaluate subexpressions first. n o Evaluate an expression tree starting from the leaves to the root. Example: (* (+ 2 3) (+ 4 5 )) n Evaluate the two additions first, then the multiplication. SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 7
Function Evaluations, cont’d o o Use the keyword quote to prevent Scheme from attempting to evaluate a list. n Example: The special form (quote (1 2 3 4)) evaluates to the list (1 2 3 4) n You can use an apostrophe instead of quote: '(1 2 3 4) The empty list: '() _ SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 8
Binding Values to Symbols o Use the define special form to bind a value to a variable. n Example: (define ten 10) Bind the integer value 10 to the symbol (variable) ten. n Example: (define Robert 'Bob) Bind the variable Robert to the symbol Bob. _ SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 9
Constructing Lists o Function cons is the constructor of lists. n Examples: (cons 1 '()) (1) (define lst 1 (cons 1 '()) (cons 2 lst 1) (2 1) lst 1 (1) (define lst 2 (cons 2 lst 1)) lst 2 (2 1) (cons 2 (cons 1 '())) (cons 'three (cons 2 (cons 1 '())))) ((2 1) three 2 1) SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 10
Deconstructing Lists o Selector function car returns the first element of a list. n Examples: (car '(1 2 3 4)) 1 (car '((ab) (cd ef) gh)) (ab) (car '(((one hit wonder)))) ((one hit wonder)) (car '(())) () SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 11
Deconstructing Lists, cont’d o Selector function cdr when applied to an argument that evaluates to a non-empty list, returns a list that is the argument list with its first element (the car) removed. n It returns “the rest of the list”. n Examples: (cdr '(1 2 3 4)) (2 3 4) (cdr '(a (b c) (d e f))) ((b c) (d e f)) (cdr '(1)) () (cdr '((1 2))) () SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 12
Deconstructing Lists, cont’d (define name-list '((Jane Doe) (John Jones))) o How can we extract the name “Doe”? (car (cdr (car name-list))) Doe o We can combine consecutive car’s and cdr’s by concatenating their a’s and d’s between the initial ‘c’ and the final ‘r’. n Up to four a’s and d’s. (cadar name-list) Doe SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 13
Deconstructing Lists, cont’d o Given (define menu '(chicken soup ice cream)) o Evaluate: (car menu) (cadr menu) (cons (cadr menu) '()) (cons (car menu) (cons (cadr menu) '())) (cddr menu) (cons (cddr menu) '()) (cons (car menu) (cons (cadr menu) '())) (cons (cddr menu) '())) SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 14
Procedures o Scheme uses the term “procedure”. n cons car cdr + - etc. are all procedures. o We implement a mathematical function with a Scheme procedure. o Procedures cons, car, and cdr do not alter their operands. _ SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 15
Dotted Pairs o If the second argument to cons is not a list, the result is a dotted pair. n A dotted pair is two objects separated by a dot and enclosed by parentheses. (cons 'a 'b) (a. b) o n Note the spaces around the dot. The first object of a dotted pair is its car, and the second object is its cdr. (car '(a. b)) a (cdr '(a. b)) b n Also: '(a. ()) (a) '(a. (b c)) (a b c) SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 16
Predicates o Scheme has a group of procedures called predicates. n n o A predicate tests its argument and returns #t or #f. By convention, a predicate’s name ends with a ? Examples: n number? (define x 56. 78) (number? (number? n symbol? 12) #t x) #t '3) #t 'x) #f (car '(1 2 3))) #t (symbol? 'x) #t (symbol? 13) #f (symbol? (cdr '(left right))) #f SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 17
Predicates, cont’d o More examples: n boolean? (boolean? (number? 'a)) #t (boolean? (cons 'a '())) #f n pair? (pair? '(alpha beta)) #t (pair? '(1)) #t (pair? '()) #f n null? (null? '()) #t (null? (cdr '(cat))) #t (null? (car '((x y z)))) #f n procedure? (procedure? cons) #t (procedure? +) #t (procedure? 123) #f SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 18
Sameness Predicates o Predicate = tests numbers (usually integers). n o Examples: (= 5 (+ 2 3)) #t (= 5 (* 2 2)) #f Predicate eq? tests symbols. n Examples: (define Garfield 'cat) (eq? o 'cat) Garfield 'cat) (car '(Garfield cat)) 'cat) (cons 1 '(2 3))) #t #t #f #f Predicate eqv? tests numbers, symbols, and booleans. n Examples: (eqv? (+ 2 3) (- 12 7)) #t (eqv? 'cat Garfield) #t (eqv? (cons 1 '(2 3))) #f SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 19
Sameness Predicates, cont’d o Predicate equal? tests all data types. n o Examples: (equal? (+ 2 3) (- 12 7)) 'cat Garfield) (cons 1 '(2 3))) (cdr '(a c d)) (cdr '(b c d))) #t #t Which sameness predicate to use? n n It is less efficient if a predicate must first determine the type of its arguments. Therefore, use the predicate designed specifically for the type of its arguments. _ SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 20
Defining Procedures o Scheme uses a lambda expression to create a procedure object. n Example: (lambda (item lst) (cons item lst)) parameters o Apply a lambda expression to some arguments. n o body Example: ((lambda (item lst) (cons item lst)) 'x '(1 2 3)) (x 1 2 3) Bind the lambda expression to a symbol in order to reuse the procedure. n Example: (define add-item (lambda (item lst) (cons item lst))) (add-item 'x '(1 2 3)) (x 1 2 3) SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 21
Defining Procedures, cont’d o Some syntactic sugar: n Instead of: (define add-item (lambda (item lst) (cons item lst))) n Use: (define (add-item 2 item lst) (cons item lst)) (add-item 2 'x '(1 2 3)) (x 1 2 3) SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 22
- Slides: 22