Scheme: Variant of LISP: cs480 (Prasad) L12Scm 1
Scheme: Variant of LISP: cs480 (Prasad) L12Scm 1
cs480(Prasad)
L12Scm
Scheme
Scheme = LISP + ALGOL
symbolic list manipulation block structure; static scoping
Symbolic Computation
Translators
Parsers, Compilers, Interpreters.
Reasoners
Natural language understanding systems Database querying
Striking Features
Simple and uniform syntax Support for convenient list processing (Automatic) Garbage Collection Environment for Rapid Prototyping
Intrinsically generic functions Dynamic typing (flexible but inefficient) Compositionality through extensive use of lists. (minimizing impedance mismatch)
cs480(Prasad) L12Scm 3
Expressions
Literals Literals Variables
Identifier represents a variable. Variable reference denotes the value of its binding. x 5 ref
Variables
Procedure calls
cs480(Prasad)
L12Scm
Scheme Identifiers
E.g., y, x5, +, two+two, zero?, list->string, etc (Illegal) 5x, y)2, ab c, etc Identifiers
reserved keywords variables Not case sensitive
cs480(Prasad)
functions = procedures
L12Scm 5
cs480(Prasad)
L12Scm
(+ 2.2+1.1i 2.2+1.1i )
cs480(Prasad)
L12Scm
Evaluation
Evaluate each element of the outerlist recursively. Apply the result of the operator expression (of function type) to the results of zero or more operand expressions.
cs480(Prasad) L12Scm 8
Lists
Ordered sequence of elements of arbitrary type (Heterogeneous)
Empty List : () 3-element list : (a b 3) Nested list : (1 (2.3 x) (a) =/= (a a) (a b) =/= (b a) ( a ) =/= ( ( a ) )
4)
Supports meta-programming
program manipulating programs
cs480(Prasad) L12Scm 9
Special Forms
Definition
(define <var> <expr>)
Conditional
(if <test> <then> <else>)! > (define false #f) > (if (symbol? a) (zero? 5) (/ 10 0) )
cs480(Prasad)
L12Scm
10
Symbols
Identifiers treated as primitive values.
Distinct from identifiers that name variables in program text. Distinct from strings (sequence of characters).
cs480(Prasad)
L12Scm
11
cs480(Prasad)
L12Scm
12
Pairs
(cons a b)
a (a . (b . ())) b Printed as: (a . b)
(cons
(cons
b ()) )
Printed as: (a b)
a
cs480(Prasad) L12Scm
()
13
(contd)
(cons a (cons a (cons b ())))
Printed as: (a a b)
a a
cs480(Prasad) L12Scm
()
14
List Functions
Operations
car, cdr, cons, null?, ... list, append, ... cadr, caddr, caaar, ...
Expressions
( length (quote (quote a)) ) ( length a ) = 2 ( length quote ) (cadar X) = (car (cdr (car X)))
cs480(Prasad) L12Scm 15
(define xl (a b c))
{ allocate storage for the list and initialize xl}
car, first :
(car xl)
cdr :
(cdr xl)
cons :
(cons a (b c))
cs480(Prasad) L12Scm
For all non-empty lists xl: (cons (car xl) (cdr xl)) = xl For all x and lists xl: (car (cons x xl)) = x (cdr (cons x xl)) = xl car : first element of the outermost list. cdr : list that remains after removing car.
(cons () ()) (cons () ())
cs480(Prasad) L12Scm
= ?? = (())
17
null? :
list :
append :
Role of parentheses
cs480(Prasad)
L12Scm
19
Equivalence Test
(eq? (cons 3 ()) (cons 3 ()))
= #f
= #f
= #t
20
(car
. cdr)
L12Scm
21
cs480(Prasad)
L13FP
22
Abstracting Expressions
cs480(Prasad)
L13FP
23
Procedural/Imperative vs Functional
Program: a sequence of instructions for a von Neumann m/c. Computation by instruction execution. Repetition: Iteration. Modifiable or updateable variables.
cs480(Prasad)
Program: a collection of function definitions (m/c independent). Computation by term rewriting. Repetition: Recursion. Assign-only-once variables.
L13FP 24
Paradigm vs Language
Procedural Style
i := 0; sum := 0; while (i < n) do begin i := i + 1; sum := sum + i end;
Functional Style
func sum(n:int) : int; begin if n = 0 then 0 else n + sum(n-1) end;
Storage efficient
No Side-effect
L13FP 26
cs480(Prasad)
Role of Variables
Imperative (read/write) i 0 1 sum 0 1 Functional (read only) 3 n1 2 n2 1 n3 2 3
6 3 1
3 6
cs480(Prasad)
L13FP
27