Kursens personal Kursens bakgrund Kursens syfte TDDC74 Programming:

advertisement
Kursens personal
TDDC74 Programming:
Abstraction and Modelling
SICP 1 – Lecture 1-3
Annika Silvervarg
Kursens bakgrund
Utvecklad på MIT
–
Ges för alla ingenjörer och datavetare
Fokus på abstraktion och modellering
Scheme “gammalt” språk väl valt för detta syfte
Kursens innehåll
SICP1
–
Abstraktioner baserat på procedurer
SICP2
–
Abstraktioner baserat på data
SICP3
–
Abstraktioner baserat på objekt
Annika Silvervarg
Anders Haraldsson
Jonas Wallgren
Peter Dalenius
Jalal Maleki
Anette Larsson
Kursens syfte
Kursens syfte är att studenterna ska utveckla
förmåga att representera och lösa problem med
hjälp av konstruktioner i datorspråk. Efter avslutad
kurs ska studenterna kunna:
redogöra för grundläggande datavetenskapliga begrepp
relaterade till programmering och programspråk, särskilt
funktionella språk
metodiskt lösa programmeringsrelaterade problem i en
interaktiv programutvecklingsmiljö
representera problem och lösningar till dessa i form av
rekursiva och iterativa algoritmer och kunna uppskatta tid- och
rymdkomplexiteten för enklare fall
konstruera abstraktioner som möjliggör design av program som
är lättförståliga, återanvändbara och kan underhållas lättare
genomföra ett mindre programmeringsprojekt
Kursens organisation
Föreläsningar
Lektioner
Laborationer
Projekt
Duggor
1
Kursens examination
Kursbok
Duggor, betyg 3,4,5
Projekt, betyg G, VG
Laborationer, betyg G (inlämnade i tid)
3+G=3
4+G=4
5+G=6
3 + VG = 4*
4 + VG = 5*
5 + VG = 5+*
* Förutsatt att laboration lämnats in i tid
Programming
Computational processes
… manipulate data
… are controlled by rules, i.e. a program/procedure
… written in a specific programming language
… in this course Scheme
Expressions
Primitive
–
eg. numbers: 1, 3.14
Elements Of Programming
Primitive expressions
Means of combination
Means of abstraction
Primitive Expressions
Constants
Names
Compound
–
–
–
eg. application of mathematical primitive procedure on
numbers: (+ 1 3.14)
(operator operand … operand)
Can be nested: (+ (* 2 3) (- 4 5))
2
Constants
truth-value: #t, #f
integer: 42, +1789, -2
rational: 3/4, -22/7
floating: 3.1415, -10e10, 2e-3
complex: 3.1+4i, 2-3i
Read-Eval-Print
Read an expression
Evaluate it according to given rules
Print the result
Names
A name is a sequence of characters that does not
constitute a number in Scheme:
+
square
week23
i-am-a-name-in-scheme-too
+inf.0
http://www.google.com/
-1+
2+2
Evaluating Numbers
Read the number
Evaluate it
Print the value
4 is read and we get ’4’ and it is evaluated to the
number 4
Evaluating Truth Values
#t and #f are the truth values in Scheme
#t represents the value ’true’
#f represents the value ’false’
Compound Expressions
Procedure Application
Special Forms
Abstraction Operators
3
Compound Expressions
Primitive Function Application
(<function> <arg1> <arg2> ...)
User-Defined Function Application
(<function> <arg1> <arg2> ...)
Special Forms:
(if <condition> <then> <else>)
(and <exp1> <exp2> ...)
(or <exp1> <exp2> ...)
Abstraction:
(define <name> <value>)
(lambda <args> <body>)
grouping together many procedures
Example Function
Domain: {..., -2, -1, 0, 1, 2, ...}
Range: {..., -2, -1, 0, 1, 2, ...}
Function: {..., < -2, 4 >, < -1, 1 >, < 0, 0 >, < 1, 1 >,...}
Functions In Mathematics
What element in R (Range) corresponds to an
element in D (Domain)
R and D are both sets
The function mapping D to R is also a set – a set of
pairs chosen from D and R
Functions In Programs
How to map elements of D to R?
Use algorithms or descriptions that do the mapping
Example:
(LAMBDA (N) (* N N)) that given N computes N*N
Example:
(LAMBDA (R) (* 2 (* 3.1415 R)))
(LAMBDA (N) (* N N))
Parameter: N
Body: (* N N)
((LAMBDA (N) (* N N)) 7)
Parameter: N
Body: (* N N)
Argument: 7
Value: 49
4
Parentheses
Parentheses are for helping us to understand the
structure of our programs, not for confusing us
((LAMBDA (N) (* N N)) 7)
At the same time we must admit they are a pain ...
but we have no choice
IF example
(define interest-rate
(lambda (amount)
(if (< amount 10000) 0 1.5)))
Logical Operators Examples
(and (> x 2) (< x 6))
(or (> x 2) (< x -2))
(not (< x 3))
Special forms
Tests
–
–
if
cond
Logical operators
–
–
–
and
or
not
COND Example
(define interest-rate
(lambda (amount)
(cond ((< amount 0) 0)
((< amount 10000) 0.5)
((< amount 50000) 1.1)
((>= amount 50000) 1.5))))
Combining Logical Operators
(or (and (> x 2) (< x 6))
(and (> x -6) (< x -2)))
(not (odd? n))
5
What Is Abstraction?
Ignoring details
Packaging details
Concentrating on the whole rather its parts
Abstraction In Scheme
Abstraction In Programming
Giving a name to 3.1415 is abstraction
Giving a name to (LAMBDA (N) (* N N)) is
abstraction
Why Abstraction?
(define pi 3.1415)
Which is better?
(define square (lambda (n) (* n n)))
(define circle-area
(lambda (r) (* pi (square r))))
(define circle-area
(lambda (r) (* pi (square r))))
Abstraction Operators
define
lambda
Block-structures: grouping procedures
(define circle-area
(lambda (r)
(* pi ((lambda (n) (* n n)) r))))
define
Is used to give names to values and procedures
For the time being, let’s just imagine that define puts
names and their values in some sort of table.
In chapter 3 of the book more detail is revealed
6
lambda-expression
(lambda <args> <body>)
Creates a function object with the given arguments
and body
Shorthand/Syntactic sugar
Naming and defining procedures can be done in one
step
(define area
(lambda (r) (* pi (square r))))
(define (area r) (* pi (square r)))
Evaluating Function Application
Substitution model:
Given a procedure call of the form:
(<function> <arg1> <arg2> ...)
–
–
Evaluate the arguments
Replace occurrences of the parameters with value of the
corresponding arguments in the body of <function> and
evaluate the resulting expression
Evaluating the Special Forms: if
(if <condition> <this> <that>)
Evaluate <condition>, if true then evaluate <this>
otherwise evaluate <that>
This is according to the applicative method
Evaluating the Special Forms: cond
(cond (predicate-1 consequence-1)
(predicate-2 consequence-2)
...
(predicate-n consequence-n))
Evaluate predicate-i, if true, evaluate consequence-i
and return its value as the value of the whole CONDexpression
Example – Substituion Model
(define fact
(lambda (n)
(if (= n 0)
0
(* n (fact (- n 1))))))
Use substitution model to find (fact 4):
7
Evaluating the Special Forms: and
Evaluating the Special Forms: or
(and <arg-1> <arg-2> ...)
(or <arg-1> <arg-2> ...)
Evaluate arguments from left to right until a #f value
reached in which case return #f, return #t otherwise
Evaluate arguments from left to right until a #t value
reached in which case return #t, return #f otherwise
8
Download