Datastructures in Racket Part 1 For todays class, were going to - - PowerPoint PPT Presentation

datastructures in racket
SMART_READER_LITE
LIVE PREVIEW

Datastructures in Racket Part 1 For todays class, were going to - - PowerPoint PPT Presentation

Datastructures in Racket Part 1 For todays class, were going to build every data structure out of three things The first is atoms These are the primitive things in the language symbol 1 These are like int and char in


slide-1
SLIDE 1

Datastructures in Racket

Part 1

slide-2
SLIDE 2
slide-3
SLIDE 3

For today’s class, we’re going to build every data structure out of three things

slide-4
SLIDE 4

The first is atoms These are the primitive things in the language

slide-5
SLIDE 5

‘symbol 1

slide-6
SLIDE 6

These are like “int” and “char” in C++

slide-7
SLIDE 7

The second is the empty list

slide-8
SLIDE 8

‘()

slide-9
SLIDE 9

The last is cons

slide-10
SLIDE 10

Cons is a function that takes two values and makes a pair

slide-11
SLIDE 11
slide-12
SLIDE 12

That pair is represented as a cons cell

slide-13
SLIDE 13

(cons 1 2)

1 2

slide-14
SLIDE 14

cons is the the natural constructor of the language

slide-15
SLIDE 15

I use two strange words to refer to the elements of this cons cell

slide-16
SLIDE 16

“car”

slide-17
SLIDE 17

“car” “cdr”

slide-18
SLIDE 18

Because car and cdr break apart what I build with cons, I call them my destructors

slide-19
SLIDE 19

And that’s all

slide-20
SLIDE 20

And that’s all

Atoms ‘sym 23 #\c Empty list ‘() cons (cons ‘sym 23) car/cdr (car (cons ‘sym 23))

slide-21
SLIDE 21

Using just this, I can make a list

slide-22
SLIDE 22

Using just this, I can make a list

(And everything else in the world, but we’ll get back to that…)

slide-23
SLIDE 23

If I want to make the list containing 2 I do this

slide-24
SLIDE 24

(cons 2 ‘())

2 ‘()

slide-25
SLIDE 25

When I do this, Racket prints it out as a list

slide-26
SLIDE 26

‘(2)

slide-27
SLIDE 27

The way to read this is “The list containing 2, followed by the empty list.”

slide-28
SLIDE 28

Just as I can build lists of a single element, I can build larger lists from smaller lists…

slide-29
SLIDE 29

And I do that by stuffing lists inside other lists…

slide-30
SLIDE 30

(cons 2 ‘())

2 ‘()

slide-31
SLIDE 31

(cons 2 ‘())

2 ‘()

(cons 3 )

3

slide-32
SLIDE 32

Racket will print this out as

slide-33
SLIDE 33

‘(3 2)

slide-34
SLIDE 34

Of course, I probably need at least numbers as primitives right?

slide-35
SLIDE 35

To get the head of a list, I use car

slide-36
SLIDE 36

(cons 2 ‘())

2 ‘()

(cons 3 )

3

slide-37
SLIDE 37

(cons 2 ‘())

2 ‘()

(cons 3 )

3

(car )

slide-38
SLIDE 38

(cons 2 ‘())

2 ‘()

(cons 3 )

3

(cdr )

slide-39
SLIDE 39

So now how would I get the second element?

slide-40
SLIDE 40

(cons 2 ‘())

2 ‘()

(cons 3 )

3

(cdr )

slide-41
SLIDE 41

(cons 2 ‘())

2 ‘()

(cons 3 )

3

(cdr ) (car )

slide-42
SLIDE 42

Racket abbreviates

(cons 1 (cons 2 (cons…(cons n ‘())…)))

as…

‘(1 2 … n)

slide-43
SLIDE 43

If I wanted to write out lists, I could do so using (cons 1 (cons 2 …))

slide-44
SLIDE 44

How do I get the nth element of a list?

slide-45
SLIDE 45

(define (nth list n) (if (= 0 n) (car list) (nth (cdr list) (- n 1))))

slide-46
SLIDE 46

Now, write (map f l)

slide-47
SLIDE 47

Writing lists would get quite laborious

slide-48
SLIDE 48

Instead, I can use the primitive function list

slide-49
SLIDE 49

(list 1 2 ‘serpico) ‘(1 2 serpico)

slide-50
SLIDE 50

Oh, and actually I can use this to represent trees too

slide-51
SLIDE 51

1 2 3 4

slide-52
SLIDE 52

How would I build this?

slide-53
SLIDE 53

(define empty-tree 'empty-tree) (define (make-leaf num) num) (define (make-tree left right) (cons left right))

slide-54
SLIDE 54

You define (left-subtree tree)

slide-55
SLIDE 55

(define (least-element tree) (if (number? tree) tree (least-element (left-subtree tree))))

slide-56
SLIDE 56

But surely I need things like numbers right?

slide-57
SLIDE 57
slide-58
SLIDE 58

It turns out, you could build those using just cons, car, cdr, if, =, and ‘()

slide-59
SLIDE 59

Define the number n as … ‘() ‘(()) ‘(() ()) …

slide-60
SLIDE 60

(define (weird-plus i j) (if (equal? i '()) j (weird-plus (cdr i) (cons '() j))))

slide-61
SLIDE 61

(weird-plus '(() ()) '(() ())) '(() () () ())

slide-62
SLIDE 62

It turns out, if I’m clever, we can even get rid of if and equal (Though we shall not do so here..)

slide-63
SLIDE 63

I can build my own datatypes in this manner

slide-64
SLIDE 64

I usually write constructor functions to help me build datatypes

slide-65
SLIDE 65

I usually write constructor functions to help me build datatypes And I usually write destructor functions to access it

slide-66
SLIDE 66

(define (make-complex real imag) (cons real imag)) And I usually write destructor functions to access it

slide-67
SLIDE 67

(define (make-complex real imag) (cons real imag)) (define (get-real complex) (car complex)) (define (get-imag complex) (cdr complex))

slide-68
SLIDE 68

Now, define (add-complex c1 c2)

slide-69
SLIDE 69

Next, define (make-cartesian x y) And the associated helper functions

slide-70
SLIDE 70

Next class we will talk about… struct match I/O And switch over to layout in assembly