$theTitle=wp_title(" - ", false); if($theTitle != "") { ?>
One Stop Computer Science
1 Jan // php the_time('Y') ?>
Introduction
Mathematical Functions
l(x) x * x * x
for the function cube(x) = x * x * x
Lambda Expression
e.g., (l(x) x * x * x)(2)
which evaluates to 8
Function Composition
LISP Data Type and Structures
e.g., (A B (C D) E)
LISP Interpretation
e.g., If the list (A B C) is interpreted as data it is
a simple list of three atoms, A, B, and C
If it is interpreted as a function application,
it means that the function named A is
applied to the two parameters, B and C
Special Form Function: DEFINE
1.To bind a symbol to an expression
e.g., (DEFINE pi 3.141593)
Example use: (DEFINE two_pi (* 2 pi))
These symbols are not variables – they are like the names bound by Java’s final declarations
2.To bind names to lambda expressions (LAMBDA is implicit)
e.g., (DEFINE (square x) (* x x))
Example use: (square 5)
– The evaluation process for DEFINE is different! The first parameter is never evaluated. The second parameter is evaluated and bound to the first parameter.
Output Function
List Functions
–QUOTE is required because the Scheme interpreter, named EVAL, always evaluates parameters to function applications before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate
–QUOTE can be abbreviated with the apostrophe prefix operator
‘(A B) is equivalent to (QUOTE (A B))
(LIST ′apple ′orange ′grape) returns
(apple orange grape)
Predicate Functions
(EQ? ‘A ‘A) yields #T
(EQ? ‘A ‘B) yields #F
(EQ? ‘A ‘(A B)) yields #F
(EQ? ‘(A B) ‘(A B)) yields #T or #F
(EQ? 3.4 (+ 3 0.4))) yields #T or #F
(EQV? 3 3) yields #T
(EQV? ‘A 3) yields #F
(EQV 3.4 (+ 3 0.4)) yields #T
(EQV? 3.0 3) yields #F (floats and integers are different)
(LIST? ‘()) yields #T
(NULL? ‘(())) yields #F
Tail Recursion in Scheme
Functional Form
–If h is the composition of f and g, h(x) = f(g(x))
(DEFINE (g x) (* 3 x))
(DEFINE (f x) (+ 2 x))
(DEFINE h x) (+ 2 (* 3 x))) (The composition)
–In Scheme, the functional composition function compose can be written:
(DEFINE (compose f g) (LAMBDA (x) (f (g x))))
((compose CAR CDR) ‘((a b) c d)) yields c
(DEFINE (third a_list)
((compose CAR (compose CDR CDR)) a_list))
is equivalent to CADDR
Comparing Functional and Imperative Language
–Efficient execution
–Complex semantics
–Complex syntax
–Concurrency is programmer designed
–Simple semantics
–Simple syntax
–Less efficient execution
–Programs can automatically be made concurrent
Leave a reply