TDDC74 Programmering: Abstraktion och modellering VT 2015 Johannes Schmidt

advertisement
TDDC74 Programmering:
Abstraktion och modellering
VT 2015
Johannes Schmidt
Institutionen för datavetenskap
Linköpings universitet
1
Lecture 3
Higher order procedures
More on syntactic sugar
Procedures as arguments
Procedures as return values
SICP 1, Del 3
2
More on syntactic sugar
let, let* is called syntactic sugar because they just
hide the equivalent and more difficult to read lambda
constructs
There is more syntactic sugar to hide lambda constructs:
define a proceure via
(define (<name> <parameter list>) <body>)
instead of
(define <name> (lambda (<parameter list>) <body>))
3
More on syntactic sugar
(define square (lambda (x) (* x x)))
is simpler written as
(define (square x) (* x x))
From now on we will primarily use this syntactic sugar version
to define procedures.
But keep in mind that it hides a lambda construct.
4
Exercise
Rewrite without syntactic sugar
(define (distance x1 y1 x2 y2)
(let ((dx (- x2 x1))
(dy (- y2 y1)))
(sqrt (+ (square dx)
(square dy)))))
5
(procedure param1 param2 …)
Until now:
parameters = primitive objects (integers, constants)
What about procedures as parameters for procedures?
(define (sum-of-some-function f x y)
(+ (f x) (f y)))
>(sum-of-some-function (lambda (x) (* x x)) 3 4)
25
6
Different sums
(define (sum-ints a b)
(if (> a b)
0
(+ a
(sum-ints (+ a 1) b))))
>(sum-ints 3 6)
18 (= (+ 3 (+ 4 (+ 5 (+ 6 0)))))
7
Different sums
(define (sum-ints a b)
(if (> a b)
0
(+ a
(sum-ints (+ a 1) b))))
(define (sum-squares a b)
(if (> a b)
0
(+ (square a)
(sum-squares (+ a 1) b))))
(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a)
(sum-cubes (+ a 1) b))))
8
Common structure and code
(define (sum-ints a b)
(if (> a b)
0
(+ a
(sum-ints (+ a 1) b))))
(define (sum-squares a b)
(if (> a b)
0
(+ (square a)
(sum-squares (+ a 1) b))))
(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a)
(sum-cubes (+ a 1) b))))
9
General sum
(define (sum-cubes a b)
(if (> a b)
0
(+ (cube a)
(sum-cubes (+ a 1) b))))
(define (sum a b term)
(if (> a b)
0
(+ (term a)
(sum (+ a 1) b term))))
10
General Sum
(define (sum-squares a b)
(sum a b square))
(define (sum-cubes a b)
(sum a b cube))
How to write sum-ints?
11
Two Kinds of Accumulation
(define (sum a b term next)
(if (> a b)
0
(+ (term a)
(sum (next a) b term next))))
(define (mult a b term next)
(if (> a b)
1
(* (term a)
(mult (next a) b term next))))
12
Similarities
(define (sum a b term next)
(if (> a b)
0
(+ (term a)
(sum (next a) b term next))))
(define (mult a b term next)
(if (> a b)
1
(* (term a)
(mult (next a) b term next))))
13
Accumulation in general
(define (acc a b term next init op)
(if (> a b)
init
(op (term a)
(acc (next a) b term next
init op))))
14
Accumulation in general
(define (acc a b term next init op)
(if (> a b)
init
(op (term a)
(acc (next a) b term next
init op))))
(define (sum-ints a b)
(acc a b id inc 0 +))
where id stands for (lambda (x) x) and
inc for (lambda (x) (+ x 1))
15
Accumulation in general
(define (acc a b term next init op)
(if (> a b)
init
(op (term a)
(acc (next a) b term next
init op))))
(define (sum-cubes a b)
(acc a b cube inc 0 +))
where cube stands for (lambda (x) (* x x x)) and
inc for (lambda (x) (+ x 1))
16
Procedures as return values
A procedure that returns a procedure?
(define (f x) (lambda (y) (* x y)))
(define (get-incrementer inc-id)
(cond ((= inc-id 0)
(lambda (x) (+ x 1)))
((= inc-id 1)
(lambda (x) (* x 2)))
(else
(lambda (x) x))))
17
Procedures as return values
(define (make-greeter name)
(lambda ()
(display "Hello ")
(display name)
(display "\n")))
(define greet-annika (make-greeter "Annika"))
(greet-annika)
Hello Annika
((make-greeter "Greta"))
Hello Greta
18
Procedures as return values
The composition of two functions f and g is defined as
f(g(x))
Define a procedure compose that implements
composition
(define (compose f g)
(lambda (x) (f (g x))))
19
Higher order procedures
A procedure is of higher order if it either has procedures as
parameters or as return values (or both)
20
Iterative Fibonacci
0, 1, 1, 2, 3, 5, 8, 13, ...
(define fib-iter
(lambda (n)
(fib2 n 0 1)))
Scope of fib2:
global
(define fib2
(lambda (n a b)
(cond
((= n 0) a)
(else (fib2 (- n 1) b (+ a b))))))
21
Iterative Fibonacci, nicer
(define fib-iter
(lambda (n)
(define fib-help
Scope of fib-help:
(lambda (n a b)
local
(cond
((= n 0) a)
(else (fib-help (- n 1) b (+ a b))))))
(fib-help n 0 1)))
22
Iterative Fibonacci, more
compact
(define (fib-iter n)
(define (fib-help n a b)
(cond
((= n 0) a)
(else (fib-help (- n 1) b (+ a b)))))
(fib-help n 0 1))
23
Where Do Algorithms Come From
Thinking and Problem Solving
Already the ancient Egyptions had kind of „algorithms“
for solving geometry problems of the pyramids
Fortunately many problems are solved already. ”Just”
translate the solutions to algorithms
Many mathematical formulas can easily be translated to
algorithmic descriptions
Some existing solutions can be modified to solve other
problems
24
Example
Equation: C/100 = (F - 32)/180
can be formulated as two algorithms that compute C or F
(define get-celsius (lambda (f)
(* 100 (/ (- f 32) 180))))
(define get-fahrenheit (lambda (c)
(+ (/ (* c 180) 100) 32)))
25
Errors
Syntactic Error
Expression impossible to evaluate
e.g. (5 + 4)
Semantic Error
evaluable (compilable), but not solving the task you want
e.g. (* 4 5) instead of (+ 4 5)
26
Summary
Expressions
Primitive
Constants
Names
Compound
Function applications
Special forms
If
Cond
Logical operators: and, or, not
Abstraction
Lambda
Define
27
Summary
Evaluation and substitution model
Procedures and processes
Recursive procedures
Recursive processes
Iterative processes
Tree-recursive processes
Scope: local and global variables
Let, Let*
Syntactic sugar
Higher order functions
Functions as arguments
Functions as returned values
28
Exercise 1.11 SICP
f(n) = n if n<3
f(n) = f(n-1)+2f(n-2)+3f(n-3) otherwise
Write procedures that solve f(n) both recursively and
iteratively
See if you can follow the patterns used in similar solutions for
comuting the Fibonnaci numbers
29
Hints
Read the course book!
Follow the coding guidelines
(labs may be refused otherwise)
30
Dugga 1
Contents: Lecture 1, 2, 3
When: Thursday, February 12th (in 2 and a half weeks)
Registration in Studentportalen
Last day for registration: February 2nd (in 1 week)
31
Download