VT 2015 TDDC74 Programmering: Abstraktion och modellering Johannes Schmidt

advertisement
TDDC74 Programmering:
Abstraktion och modellering
VT 2015
Johannes Schmidt
Institutionen för datavetenskap
Linköpings universitet
1
Lecture 4
List structures
*
*
*
*
Symbols, Pairs, Lists
Representation of pairs, graphical notation for paris
Representation of lists with pairs
Typical list processing procedures
* SICP 2, Del 1
Earlier


Until now we wrote procedures that
work with numbers: Chapter 1 in the
course book
The intention was to be able to
concentreate on procedures and the
processes they generate
3
Primitive data: Numbers,
Symbols, Strings, ...




Numbers: 2, 3.14
Symbols: x, y, square, pi
Strings: ”whatever”, ”123”, ”\””
Characters: #\2, #\c, #\C, #\$
4
What kind of primitive data
do we have?





Procedures that test:
Number: number?
Symbols: symbol?
Strings: string?
Characters: char?
5
Compare primitive data




Numbers: =
Symbols: eq?
Strings: string=?, string<?,
string<=?, ...
Characters: char=?, char<?, ...
6
Symbols and quote




We have to differenciate between
symbols as names and symbols as data
quote is a specialform to declare that
its argument is data
Names stand for something else,
symbols stand for themselves
Usage: (quote square)
or alternatively
'square
7
Symbols and quote


(quote x) evaluates to the symbol x,
but x evaluates to the value that x is
set to.
> (define x 23)
> (quote x)
x
> x
23
8
Operations of interest –
structured data / compound data
 Construction
 Selection
of parts
 Recognition
and comparison
 Modification
of parts (chapter 3 SICP, FÖ 7-9)
 Presentation
or printing
9
Pair



Construction
Graphical representation
Printing
10
Pair




Has two parts "car" and "cdr" (could-er)
Is constructed by the procedure cons that
takes two arguments, creates the pair and
returns a reference to the pair
The procedures car and cdr are used to
access the desired part
pair? Is used for recognition
cons = "glue"
11
Graphical representation of a pair
12
Graphical representation of a pair
(cons 10 20)
10 20
13
Graphical representation of a pair
- alternative
(cons 10 20)
10 20
14
Graphical representation of a pair
(cons 10 (cons 20 30))
10
20 30
15
Graphical representation of a pair
- alternative
(cons 10 (cons 20 30))
10
20 30
16
Graphical representation of a pair
(define x (cons 10 (cons 20 30)))
x
10
20 30
17
Graphical representation of a pair
(define x
(cons (cons 10 40)
(cons 20 30)))
x
10 40
20 30
Every call to cons creates a box with two parts
18
Lists in Scheme




Usually, a list has a certain number of
elements (n) where every element is placed
in a cons-cell
A special case, called null-list or empty list,
is designated by null or '()
The ith pair contains the ith element as its
"car" while its "cdr"-part referes to the
next pair
The last cdr-cell has nothing to refer to,
so it is set to '()
19
A list is created with the
procedure cons
(cons 1
(cons 2
(cons 3 '())))
Creates the list (1 2 3)
20
The procedure list
(list 1 2 3)
is a shorthand for
(cons 1
(cons 2
(cons 3 '())))
21
Further examples and notation
(list 'this 'that)
Creates the list (this that)
Instead of (list 'this 'that)
you can also write '(this that).
22
Further examples and notation
More generally
'(p1 p2 p3 p4 …)
is a shorthand for
(list 'p1 'p2 'p3 'p4 …)
23
Lists in Racket
Lists are printed like this:
'(1 2 3)
'(0 1 1 2 3)
'(6 28)
I.e., the quotation mark, then a left
parenthesis followed by the elements and
then a right parenthesis
24
No List

The following structures are different:
(list 'a 'b 'c)
(cons 'a (cons 'b 'c))

The first expression creates a list while
the second one creates an arbitrary
structure.
25
null? and list?


null? tests if we have the empty list
list? tests if we have a list
An empty list is of course also a list
26
Test if something is a list


null? tests if we have the empty list
list? tests if we have a list
(null? '())
(list? '())
is #t
is #t
(null? (list 1 2))
(list? (list 1 2))
is #f
is #t
(list? (cons 1 2)) is #f
(list? (cons 1 '()) is #t
27
Graphical notation for lists
empty list: '() or ()
List with 1 element:
(list 10)
2 elements:
(list 10 20)
10 ()
10
20 ()
28
Graphical notation for lists general
A list that contains one or more elements
is represented by a sequence of conscells as follows:
e1
e2
...
eN ()
29
OBS!
(null? (list '())) is #f
() ()
(list '()) is the same as
(cons '() '())
30
Lists can contain lists
'((1 2) (a (b) c))
()
1
2
()
a
c
b
()
31
()
Lists can contain lists
'((1 2) (a (b) c))
Main List
()
Sublists
1
2
()
Sublist of Sublist
a
c
b
()
32
()
cons-cells can be referred to
from different places
(define pair (cons 1 2))
(define pairpair (cons pair pair))
pairpair
pair
1
2
33
cons-cells can be referred to
from different places
(define pair (cons 1 2))
(define pairpair (cons pair pair))
(define metoo pair)
pairpair
metoo
pair
1
2
34
cons-cells can be referred to
from different places
(define pair (cons 1 2))
(define pairpair (cons pair pair))
(define metoo pair)
(define andme
(cons pairpair metoo))
pairpair
andme
metoo
pair
1
2
35
Printing in Racket
Pairs (or cons-cells) are printed in the
so-called dot-notation:
(cons 1 2) as
'(1 . 2)
(cons (cons 1 2) (cons 3 4)) as
'((1 . 2) 3 . 4)
(cons 1 (cons 2 '())) as
'(1 2)
36
OBS!
Dot-notation sometimes surprises:
(cons (cons 1 2) (cons 3 4))
prints as
'((1 . 2) 3 . 4)
and not as
'((1 . 2) . (3 . 4))
37
Lists – Printing in Racket
Lists are printed according to the
following examples:
'()
'(1 2 3)
'(0 1 1 2 3)
'(6 (28 (486)))
38
Sequences
A sequence is an ordered set of 0 or more
elements
 Example:
1, 2, 3, ...
0, 1, 1, 2, 3, ...
6, 28, ...
 Can be represented by lists in Scheme,
e.g. the empty sequence is the empty list

39
Example program




Compute the length of a list
Square all elements of a list
Put two lists together
Substitute value in a list
Programs that handle lists have much in
common!
40
Program pattern: #1
work with sequences
Two cases:
 Sequence is empty: '()
 One or more elements: '(a b c)
41
Program pattern: #1
Lists as sequences.
Recursive processes.
(DEFINE (FN SEQ)
(IF (NULL? SEQ)
<null-result>
(<operation> (FN (CDR SEQ)))))
(define (mylength seq)
(if (null? seq)
0
(+ 1 (mylength (cdr seq)))))
42
Program pattern: #1
Lists as sequences.
Recursive processes.
(DEFINE (FN SEQ)
(IF (NULL? SEQ)
<null-result>
(<operation> (FN (CDR SEQ)))))
(define (square-all seq)
(if (null? seq)
'()
(cons (square (car seq))
(square-all (cdr seq)))))
43
Program pattern: #1
Lists as sequences.
Recursive processes.
(DEFINE (FN SEQ)
(IF (NULL? SEQ)
<null-result>
(<operation> (FN (CDR SEQ)))))
(define (append-lists seq-1 seq-2)
(if (null? seq-1)
seq-2
(cons (car seq-1)
(append-lists (cdr seq-1)
seq-2))))
44
Program pattern: #1
Lists as sequences.
Recursive processes.
(define (substitute seq old new)
(cond ((null? seq) '())
((eq? (car seq) old)
(cons new (substitute (cdr seq) old new)))
(else (cons (car seq)
(substitute (cdr seq) old new)))))
45
Program pattern: #1
Lists as sequences.
Recursive processes.
(define (substitute seq old new)
(if (null? seq)
'()
(cons
(if (eq? (car seq) old)
new
(car seq))
(substitute (cdr seq) old new))))
46
Compare: iterative processes
(define (member? el seq)
(cond ((null? seq) #f)
((eq? el (car seq)) #t)
(else (member? el (cdr seq)))))
47
Compare: iterative processes
(define (get-position el seq)
(define (helper seq k)
(cond ((null? seq) -1)
((eq? el (car seq)) k)
(else (helper (cdr seq)
(+ k 1)))))
(helper seq 0))
48
Download