TDDC74 Programmering: Abstraktion och modellering vt 2014

Transcription

TDDC74 Programmering: Abstraktion och modellering vt 2014
TDDC74 Programmering:
Abstraktion och modellering
VT 2015
Johannes Schmidt
Institutionen för datavetenskap
Linköpings universitet
1
Lecture 1
Introduction to the course
Scheme basics
Use of svn
SICP 1, Del 1
2
Staff
Examiner and course leader: Johannes Schmidt
Courseassistent : Anders Märak Leffler
Other teachers: Rasmus Andersson, Johan
Billman, Linnea Faxén, Christoph Heilmair, Denise
Härnström, Joakim Johnander, Helena Kihlström,
Andreas Norrstig
Kursadministratör : Anna Grabska Eklund
Studierektor : Jalal Maleki
3
Purpose
The course aims to develop the students’ ability to
represent and solve problems using constructs in
computer language. After completing the course
students should be able to:
explain basic computer science concepts related to programming and
programming languages​​, especially functional languages
methodically solve programming related problems in an interactive software
development environment
represent problems and its solutions in the form of recursive and iterative
algorithms and be able to estimate time and space complexity for simple
cases
construct abstractions that enable the design of applications that are easy
to understand, reusable and can be maintained more easily
carry out a small programming project
4
Content
SICP1
Abstractions based on procedures
SICP2
Abstractions based on data
SICP3
Abstractions based on objects
5
Svengelska?
Course language is Swedish
But lectures are given in English
Once the course has been given entirely in
English => some material is still in English
Program code is usually in English
6
Organisation
Föreläsningar (Lectures)
VT1
Lektioner (Tutorials)
VT1
Laborationer (Labs)
VT1 (+ VT2)
Projekt
VT2
Duggor / Tentamen
VT1+VT2
Course Webpage: http://www.ida.liu.se/~TDDC74/
Contents of Lectures and Tutorials will be online.
7
Examination
TEN1 written exam / duggor (U, 3, 4, 5) 2 hp
LAB1 labs (U, G) 3 hp
PRA1 project (U, 3, 4, 5) 3 hp
The final grade is primarily based on the grade of the
duggor / exam. I can be improved if
you pass all duggor,
the project grade is higher than the duggor grade
all labs and the project are done in time
8
Organisation of the Tutorials
You are divided into 5 classes, basically according to
your program:
Class
Teacher
Y1.a
Y1.b
Y1.c
MED1
MAT1/Yi1
Johannes Schmidt
Anders Märak Leffler
Christoph Heilmair
Johan Billman
Rasmus Andersson
9
Organisation of labs
The classes are subdivided into lab groups:
Labgroup
Assistant
Class-Teacher
Y1A-GR1
Y1A-GR2
Johannes Schmidt
Joakim Johnander
Johannes Schmidt
Johannes Schmidt
Y1B-GR1
Y1B-GR2
Anders Märak Leffler
Linnea Faxén
Anders Märak Leffler
Anders Märak Leffler
Y1C-GR1
Y1C-GR2
Christoph Heilmair
Andreas Norrstig
Christoph Heilmair
Christoph Heilmair
MED1-GR1
MED1-GR2
Johan Billman
Denise Härnström
Johan Billman
Johan Billman
MAT1
MED1-YI-BL
Rasmus Andersson
Helena Kihlström
Rasmus Andersson
Rasmus Andersson
10
Organisation of labs
To join the labgroups, you have to sign up in Webreg:
https://www.ida.liu.se/webreg3/
Prepare lab assignments in groups of two
-> team up with a partner
In order to sign up in Webreg, you have to be registered for the
course first (Studentportalen)!
Webreg sign up:
• Login with your liu-account
Webreg sign up
• Choose TDDC74
• Make sure TDDC74-2015 is selected
Deadline:
• Choose LAB1
January 26th!
• Choose SIGN UP
• Sign up for a corresponding labgroup (together with your partner)
11
Organisation of labs
• Prepare the lab assignments in groups of two
• Follow the coding-guidelines (course Webpage -> Laborationer)
• lab 0 is online (course Webpage -> Laborationer)
Do not forget STONE http://www.ida.liu.se/stone/main
• Work it through, show it to your lab-assistant during your first lab
session
• After that, there are 4 mandatory labs
(Expect to need more time than scheduled for the lab
sessions!)
• Prepare them and present your solutions to your Assistant and get
it validated (G = godkänt)
• If you hand in a correct lab in time (see course Webpage for
Deadlines), you may get a GT = godkänt i tid
12
Course book SICP
(Big parts of) the
three first chapters
make the content of
this course
Online version:
mitpress.mit.edu/sicp/
Read the book!
Read the coding-guidelines!
13
DrRacket
The system we will apply is DrRacket
www.racket-lang.org or www.drracket.org
14
Programming
Computational processes (Beräkningsprocesser)
… manipulate data
… controlled by rules, i.e., a program/procedure
… written in a programming language
(Scheme in this course)
15
Scheme: the language‘s elements
primitive expressions (primitiva uttryck)
means of combination (Sammansättningsregler)
means of abstraction (abstraktioner)
form or syntax
meaning or semantic
16
Expressions (uttryck)
Primitive expressions (constants, names)
1, 3.14, pi, +
Combined expressions (procedure calls)
(* pi 2)
Possible nesting: (+ (* 2 3) (- 4 5))
Operators fist --- prefixnotation (compare with
infixnotation)
(operator operand … operand)
17
Constants
truth-value: #t, #f
(sanningsvärde)
integer: 42, +1789, -2
(heltal)
rational: 3/4, -22/7
(bråk)
floating: 3.1415, -10e10, 2e-3
(flyttal)
complex: 3.1+4i, 2-3i
(komplext tal)
18
Names
A name is a sequence of characters that do
not represent a number or a reserved
syntactical object. A name is used to assign a
name to a value or a procedure. Example:
+
square
week23
i-am-a-name-in-scheme-too
http://www.google.com/
-1+
2+2
19
Interpretation: Read-Eval-Print
Read --- read an expression
Evaluate --- interprete an expression
Print --- write the result
(+ 2 (* 3 4))
14
(define x 3)
(+ 2 x)
5
20
Combined expressions
application of built in procedures
(<sysfn> <arg1> <arg2> ...)
application of user defined procedures
(<userfn> <arg1> <arg2> ...)
special forms
(if <condition> <then> <else>)
(and <exp1> <exp2> ...)
(or <exp1> <exp2> ...)
means of abstration
(define <name> <value>)
(lambda <args> <body>)
21
Functions in Mathematics
D, Domain: {..., -2, -1, 0, 1, 2, ...}
R, Range: {..., -2, -1, 0, 1, 2, ...}
Function from D to R, f: D -> R:
{..., < -2, 4 >, < -1, 1 >, < 0, 0 >, < 1, 1 >,...}
f(x) = x2
22
Functions in Scheme
A machine that maps D to R?
Apply precise descriptions (algorithms) for the
computation
Example:
(lambda (n) (* n n))
Lambda calculus notation:  n . n2
Example:
(lambda (r) (* 2 (* pi r)))
23
Lambda abstraction
(lambda (n) (* n n))
Define a procedure that computes the square of its
parameter
Parameter: (n)
Body: (* n n)
24
Procedure application
((lambda (n) (* n n)) 7)
A procedure with the parameter n is applied on the
argument 7
Parameter: n
Body: (* n n)
Argument: 7
Value: 49
25
Parentheses
Parentheses are there to define the structure of a combined
expression or description
((lambda (n) (* n n)) 7)
26
Special forms
Testing
if
cond
Logical operations
and
or
27
if
(if <predicate> <then> <else>)
(if (< amount 10000)
0.5
1.5))
If the amount is smaller than 10000 then the
expression shall give 0.5, otherwise 1.5
28
COND
(cond
(<predicate-1> <consequence-1>)
(<predicate-2> <consequence-2>)
...
(<predicate-n> <consequence-n>))
(lambda (amount)
(cond
((< amount 0) 0)
((< amount 10000) 0.5)
((< amount 50000) 1.5)
((>= amount 50000) 2.5)))
29
Convention
(lambda (amount)
(cond
((< amount 0) 0)
((< amount 10000) 0.5)
((< amount 50000) 1.5)
((>= amount 50000) 2.5)))
(lambda (amount)
(cond
((< amount 0) 0)
((< amount 10000) 0.5)
((< amount 50000) 1.5)
(else 2.5)))
30
Example of logical operators
(and <e1>,…, <en>)
(or <e1>,…, <en>)
(not <e1>)
(and (> x 2) (< x -2))
(or (> x 2) (< x -2))
(not (< x 3))
31
Combination of logical operators
(or (and (> x 2) (< x 6))
(and (> x -6) (< x -2)))
(not (or (> x 1) (< x -1)))
32
Common pitfalls
(if (odd? x) #t #f)
can be shortend to (odd? x)
(if (odd? x) #f #t)
can be shortened to (not (odd? x))
33
What is abstraction?
Giving names
Hide details
Package/bundle details
Focus on the whole instead of details
34
‘Chair’ is a linguistical abstraction
35
Abstraction in programming
Give names, e.g. assign the name pi to 3.1415
Name procedures, e.g. call (lambda (n) (* n n))
square
36
Abstraction i Scheme
(define ten 10)
(define square (lambda (n) (* n n)))
(define circle-area
(lambda (r)
(* pi (square r))))
37
Operators for Abstraction
define
lambda
Grouping of procedures
38
define
Used to assign names to constants or procedures
As for now, we can assume that define has access to
tables where the names and associations are stored
39
Lambda expression
(lambda <parameters> <body>)
Creates an entity that can compute things such as
mathematical functions
40
Naming of functions
(define area
(lambda (r) (* pi (square r))))
41
Evaluation of a function call
To evaluate a call like
(<fn> <arg1> <arg2> ...)
Evaluate <fn> to the procedure object it represents
Evaluate the arguments <arg1> <arg2> ...
Replace occurences of the parameters in the procedure
objects body with corresponding arguments and evaluate
then the body
This is called applicative substitution model for
computation
42
Special form if
While all function calls are evaluated according to the
same rules, special forms are treated a little different
E.g. (if <predicate> <then> <else>)
Evaluate <predicate>, if true, evaluate <then>,
otherwise evaluate <else>
The result is either the <then> or the <else>
expressions value
43
Special form COND
(cond
(<predicate-1> <consequence-1>)
(<predicate-2> <consequence-2>)
...
(<predicate-n> <consequence-n>))
Evaluate <predicate-1>, if true, evaluate
<consequence-1>, otherwise go to the next predicate
and continue.
At least one predicate must be true.
44
Special form and
(and <arg-1> <arg-2> ...)
Evaluate the argument from left to right until a false
value #f occures, in this case this is the value of the
whole and-expression, otherwise #t.
45
Special form or
(or <arg-1> <arg-2> ...)
Evaluate the arguments from left to right until a true
value #t occurs, in this case this is the value of the
whole or-expression, otherwise #f
46
The following is equivalent
(cond
((> x 0) x)
((= x 0) 0)
((< x 0) (- x)))
(if (> x 0)
x
(if (= x 0)
0
(- x)))
47
Distance between two points
(define distance
(lambda (x1 y1 x2 y2)
(sqrt
(+ (square (- x2 x1))
(square (- y2 y1))))))
(define square
(lambda (n) (* n n)))
48
Distance between two points
(define distance (lambda (x1 y1 x2 y2)
(sqrt (+ (square (- x2 x1)) (square (- y2 y1))))))
(define square (lambda (n) (* n n)))
Applicative substitution model:
(distance 1 1 4 5)
(sqrt (+ (square (- 4 1)) (square (- 5 1))))
(sqrt (+ (square 3) (square 4)))
(sqrt (+ (* 3 3) (* 4 4)))
(sqrt (+ 9 16))
(sqrt 25)
5
49
Special names
In this course the names foo and bar are often used to
name things for illustrative / educational purpose. No
particular meaning is associated with these names.
Read up on wikipedia for more information on foo and bar
50
Code submission and svn
Detailed information on svn and code submission in this course can
be found on the course pages, e.g.
http://www.ida.liu.se/~TDDC74/labbar/subversion.sv.shtml
http://www.ida.liu.se/~TDDC74/labbar/inlamning.sv.shtml
See also svn FAQ
http://www.ida.liu.se/~TDDC74/labbar/inlamning-faq.sv.shtml
51
What is svn?
svn = subversion
= a software versioning and revision control system.
Such a system organizes and renders easier the work of several
users working on the same files.
A central version of the files is kept on an (svn-) server.
Users work on so called working copies
Synchronization of the work is done with help of the svn system.
52
Setting up svn
1. In the terminal execute
module initadd prog/subversion
This will make the svn command permanently available on your
account.
53
Setting up svn
2. Initially a Checkout is needed. This creates a working copy.
Move to your folder,
e.g. user@computer:~/kurs/tddc74/labs$,
and execute
svn checkout your-url .
your-url = e.g. https://svn-und.ida.liu.se/courses/TDDC74/2014-1-LAB1/y1a-gr1-1
Do not forget the whitespace and the dot after the url
54
Setting up svn
3. Run the script on ~TDDC74/setup
This creates the folders
lab1, lab2, lab3, lab4,
redovisning, komplettering, ok,
you will work with
and gets you some given files for lab1 and lab2.
55
Setting up svn
4. Execute
svn add *
This will prepare all the folders and files to be part of the svn
system.
5. Execute
svn commit –m "Some comment, e.g. Initial commit"
This will finally upload the files to svn.
56
How to work with it
1. The folders purpose.
The folder labX is your working folder for lab X.
When you want to hand in your solution to your lab assistant,
copy it to the folder redovisning, svn-add it and svn-commit.
If accepted by your lab assistant he will move it to the folder ok.
Otherwise to the folder komplettering.
Feedback on what is not ok will be given either via webreg,
or also in a text file in the komplettering folder.
57
How to work with it
2. How do I know when my lab assistant has accepted
my hand in or given me a komplettering?
You have to execute the command
svn update
This will show you changes your lab assitant (or anybody else)
has done to your files / folder.
58
To keep in mind
1. It is a good idea always to svn update before you
(re-)start working. Otherwise you might work on out-ofdate files.
2. When your stop working (temporarily), it is always a good
idea to svn commit, in order to "save" your changes to
the svn.
3. When your have created a new file, this file needs first to
be svn added, before an svn commit will upload it to
svn.
4. Which files are currently "added" can be seen with the
command svn list
59