Transcript val a = 15

A global environment
val a = 15
val b = "foo";
val c = fn (n, p)
=> p = n * p;
val d = [1,2];
Title:
ml-environment.eps
Creator:
fig2dev Version 3.2.3 Patchlevel
Prev iew :
This EPS picture w as not s av ed
w ith a preview inc luded in it.
Comment:
This EPS picture w ill print to a
Pos tSc ript printer, but not to
other ty pes of printers.
Let and local environments
let
val a = 20
in
hd(d) + a
end;
Title:
ml-let-environment.eps
Creator:
fig2dev Version 3.2.3 Patchlevel
Prev iew :
This EPS picture w as not s av ed
w ith a preview inc luded in it.
Comment:
This EPS picture w ill print to a
Pos tSc ript printer, but not to
other ty pes of printers.
Function expressions generate closures
fun f(x, y) =
let fun nested(z) = a + x + z
in nested(y) end;
Title:
ml-fn-environment-1.eps
Creator:
fig2dev Version 3.2.3 Patc hlev el
Preview :
This EPS picture w as not saved
w ith a preview included in it.
Comment:
This EPS picture w ill print to a
PostScript printer, but not to
other ty pes of printers .
Function application 1
f(2, 10)
Title:
ml-fn-env ironment-2.eps
Creator:
fig2dev Version 3.2.3 Patchlevel
Prev iew :
This EPS picture w as not s av ed
w ith a preview inc luded in it.
Comment:
This EPS picture w ill print to a
Pos tSc ript printer, but not to
other ty pes of printers.
Nested aplication
let
fun nested(z) =
a + x + z
in
nested(y)
end
Title:
ml-fn-env ironment-3.eps
Creator:
fig2dev Version 3.2.3 Patchlevel
Prev iew :
This EPS picture w as not s av ed
w ith a preview inc luded in it.
Comment:
This EPS picture w ill print to a
Pos tSc ript printer, but not to
other ty pes of printers.
Closures as parameters
val a = 15;
fun addA(k) = k + a;
fun doAction(f, a) = f(a);
Closures as return values
fun makeAddX(x) = fn y => x + y;
val add5 = makeAddX 5;
val elevn = add5 6;
lambda = let
let
val x = 3;
val y = 4;
in
x + y
end;
Scheme
• Expressions: atoms or (parenthesized lists)
• Evaluator:
– Parse an expression
– If expression is (bound to) atom, return it
– Otherwise, expression is a list with head V:
• If V is a special form, do the special form
• Otherwise, evaluate elements in tail and apply V to tail.
Scheme examples
(define a 15)
; val a = 15
(define d '(1 2))
; val d = [1, 2]
(let* ((a 20)) (+ (car d) a))
let val a = 20 in hd(d) + a;
(define (f x y)
(let* ((nested (lambda (z) (+ a x z))))
(nested y)))
fun f(x, y) =
let val nested = fn z => a + x + z;
in nested y;
Scheme dynamic typing
• Types are "latent"
• Types of values are irrelevant (unchecked)
until you use them.
• Typing is still strong: attempting to perform
an invalid operation will raise a runtime
type error
Objects are higher-order
functions
(define (make-student name id-num)
(lambda (message)
(cond ((equal? message 'getName) name)
((equal? message 'getID)
id-num)
((equal? message 'cloneMe)
(make-student (name id-num)))
(else "INVALID MESSAGE"))))
(define alice (make-student "Alice" 123456))
(display (alice 'getName))
Programs as data
; Evaluate list structure as if it were
; a program
(eval (list '+ 1 2 3))
; Single quote recursively prevents
; evaluation of sublists
(define mystuff '(* (+ 1 2 3)))
Why functional programming?
• Reason 1: influence on other languages
– Understand languages of present
– Modern languages borrow heavily from ideas invented in
functional languages
– Be prepared for languages of the future
– Not everyone retires by 35
– New languages come along all the time
– Learn concepts once, reuse anywhere
Why functional programming?
• Reason 2: Functional style enhances clarity
(when used appropriately)
– Avoid side effects
– Prefer return values to output parameters & side effects.
Try to preserve "substitutability". Take advantage of
constant data.
– Use higher-order thinking
– parameterize fns with fns for flexibility and reuse
– Polymorphic types
– Think of operations that are operate over "generic" types.
Why functional programming?
• Reason 3: It's cool.
– Programming is a medium for expressing ideas
– Functional languages are powerful, flexible,
and expressive.
– You are not just a code monkey.