Download presentation
Presentation is loading. Please wait.
Published byGeorgiana Beasley Modified over 8 years ago
1
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
2
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 2 Scheme Language Elements All programs and data in Scheme are expressions. Two types of expressions: atoms and lists Atom Lowest level element Constant (number, string, character, boolean) Symbols (identifiers) List One or more expressions separated by spaces. All surrounded by a set of parentheses. _
3
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 3 Scheme Language Elements Atoms Lists 42 an integer value 2.1 a real value alpha a symbol "hello" a string value #\a the character ‘a’ #T the Boolean value true (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
4
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 4 Scheme Evaluation Rule An atomic literal evaluates to itself. Example: 42 42 A non-keyword symbol evaluates to the value to which it is bound. 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. 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)
5
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 5 Scheme Evaluation Rule: Lists Special form: The first list item is a keyword. Use a special evaluation rule, depending on the keyword. Example: The define and let special forms. Otherwise, the list is a function application. 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. _
6
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 6 Scheme Functions Expressions are lists written in prefix form. Example: (+ 2 3) Apply function + to the values 2 and 3 and return the value 5. Distinguish: The function itself, as an object. A call to the function, written as a list expression. The return value of a function call.
7
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 7 Function Evaluations Recursively evaluate subexpressions first. Evaluate an expression tree starting from the leaves to the root. Example: (* (+ 2 3) (+ 4 5 )) Evaluate the two additions first, then the multiplication.
8
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 8 Function Evaluations, cont’d Use the keyword quote to prevent Scheme from attempting to evaluate a list. Example: The special form (quote (1 2 3 4)) evaluates to the list (1 2 3 4) You can use an apostrophe instead of quote : '(1 2 3 4) The empty list: '() _
9
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 9 Binding Values to Symbols Use the define special form to bind a value to a variable. Example: (define ten 10) Bind the integer value 10 to the symbol (variable) ten. Example: (define Robert 'Bob) Bind the variable Robert to the symbol Bob. _
10
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 10 Constructing Lists Function cons is the constructor of lists. Examples: (define lst1 (cons 1 '()) (cons 2 lst1) (2 1) lst1 (1) (define lst2 (cons 2 lst1)) lst2 (2 1) (cons 1 '()) (1) (cons (cons 2 (cons 1 '())) (cons 'three (cons 2 (cons 1 '())))) ((2 1) three 2 1)
11
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 11 Deconstructing Lists Selector function car returns the first element of a list. Examples: (car '(1 2 3 4)) 1 (car '((ab) (cd ef) gh)) (ab) (car '(((one hit wonder)))) ((one hit wonder)) (car '(())) ()
12
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 12 Deconstructing Lists, cont’d 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. It returns “the rest of the list”. 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))) ()
13
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 13 Deconstructing Lists, cont’d How can we extract the name “Doe”? 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 ’. Up to four a ’s and d ’s. (define name-list '((Jane Doe) (John Jones))) (car (cdr (car name-list))) Doe (cadar name-list) Doe
14
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 14 Deconstructing Lists, cont’d Given Evaluate: (car menu) (cadr menu) (cons (cadr menu) '()) (cons (car menu) (cons (cadr menu) '())) (cddr menu) (cons (cddr menu) '()) (cons (cons (car menu) (cons (cadr menu) '())) (cons (cddr menu) '())) (define menu '(chicken soup ice cream))
15
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 15 Procedures Scheme uses the term “procedure”. cons car cdr + - etc. are all procedures. We implement a mathematical function with a Scheme procedure. Procedures cons, car, and cdr do not alter their operands. _
16
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 16 Dotted Pairs If the second argument to cons is not a list, the result is a dotted pair. A dotted pair is two objects separated by a dot and enclosed by parentheses. Note the spaces around the dot. The first object of a dotted pair is its car, and the second object is its cdr. Also: (cons 'a 'b) (a. b) (car '(a. b)) a (cdr '(a. b)) b '(a. ()) (a) '(a. (b c)) (a b c)
17
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 17 Predicates Scheme has a group of procedures called predicates. A predicate tests its argument and returns #t or #f. By convention, a predicate’s name ends with a ? Examples: number? symbol? (define x 56.78) (number? 12) #t (number? x) #t (number? '3) #t (number? 'x) #f (number? (car '(1 2 3))) #t (symbol? 'x) #t (symbol? 13) #f (symbol? (cdr '(left right))) #f
18
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 18 Predicates, cont’d More examples: boolean? pair? null? procedure? (boolean? (number? 'a)) #t (boolean? (cons 'a '())) #f (pair? '(alpha beta)) #t (pair? '(1)) #t (pair? '()) #f (null? '()) #t (null? (cdr '(cat))) #t (null? (car '((x y z)))) #f (procedure? cons) #t (procedure? +) #t (procedure? 123) #f
19
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 19 Sameness Predicates Predicate = tests numbers (usually integers). Examples: Predicate eq? tests symbols. Examples: Predicate eqv? tests numbers, symbols, and booleans. Examples: (= 5 (+ 2 3)) #t (= 5 (* 2 2)) #f (define Garfield 'cat) (eq? 'cat 'cat) #t (eq? Garfield 'cat) #t (eq? (car '(Garfield cat)) 'cat) #f (eq? (cons 1 '(2 3)) (cons 1 '(2 3))) #f (eqv? (+ 2 3) (- 12 7)) #t (eqv? 'cat Garfield) #t (eqv? (cons 1 '(2 3)) (cons 1 '(2 3))) #f
20
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 20 Sameness Predicates, cont’d Predicate equal? tests all data types. Examples: Which sameness predicate to use? 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. _ (equal? (+ 2 3) (- 12 7)) #t (equal? 'cat Garfield) #t (equal? (cons 1 '(2 3)) (cons 1 '(2 3))) #t (equal? (cdr '(a c d)) (cdr '(b c d))) #t
21
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 21 Defining Procedures Scheme uses a lambda expression to create a procedure object. Example: Apply a lambda expression to some arguments. Example: Bind the lambda expression to a symbol in order to reuse the procedure. Example: (lambda (item lst) (cons item lst)) parametersbody ((lambda (item lst) (cons item lst)) 'x '(1 2 3)) (x 1 2 3) (define add-item (lambda (item lst) (cons item lst))) (add-item 'x '(1 2 3)) (x 1 2 3)
22
SJSU Dept. of Computer Science Spring 2014: February 12 CS 152: Programming Language Paradigms © R. Mak 22 Defining Procedures, cont’d Some syntactic sugar: Instead of: Use: (define add-item (lambda (item lst) (cons item lst))) (define (add-item2 item lst) (cons item lst)) (add-item2 'x '(1 2 3)) (x 1 2 3)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.