Data Structures in Racket Part 2 Last time (car (cdr (cons 3 - - PowerPoint PPT Presentation

data structures in racket
SMART_READER_LITE
LIVE PREVIEW

Data Structures in Racket Part 2 Last time (car (cdr (cons 3 - - PowerPoint PPT Presentation

Data Structures in Racket Part 2 Last time (car (cdr (cons 3 (cons 2 ()) ) ) ) 3 2 () This time Use struct to define a new datatype (struct empty-tree ()) (struct leaf (elem)) (struct tree (left right)) Copy these (struct


slide-1
SLIDE 1

Data Structures in Racket

Part 2

slide-2
SLIDE 2

Last time

slide-3
SLIDE 3

(cons 2 ‘())

2 ‘()

(cons 3 )

3

(cdr ) (car )

slide-4
SLIDE 4

This time

slide-5
SLIDE 5
slide-6
SLIDE 6

Use struct to define a new datatype

slide-7
SLIDE 7

(struct empty-tree ()) (struct leaf (elem)) (struct tree (left right))

slide-8
SLIDE 8

(struct empty-tree ()) (struct leaf (elem)) (struct tree (value left right))

Copy these

slide-9
SLIDE 9

(empty-tree) (leaf 23) (tree 12 (empty-tree) (leaf 23))

slide-10
SLIDE 10

Racket automatically generates helpers… tree? tree-left tree-right

slide-11
SLIDE 11

Write max-of-tree

Use the helpers

slide-12
SLIDE 12

Pattern matching

slide-13
SLIDE 13

Pattern matching allows me to tell Racket the “shape” of what I’m looking for

slide-14
SLIDE 14

Manually pulling apart data structures is laborious

slide-15
SLIDE 15

(define (max-of-tree t) (match t [(leaf e) e] [(tree v _ (empty-tree)) v] [(tree _ _ r) (max-of-tree r)]))

slide-16
SLIDE 16

(define (max-of-tree t) (match t [(leaf e) e] [(tree v _ (empty-tree)) v] [(tree _ _ r) (max-of-tree r)]))

Variables are bound in the match, refer to in body

slide-17
SLIDE 17

(define (max-of-tree t) (match t [(leaf e) e] [(tree v _ (empty-tree)) v] [(tree _ _ r) (max-of-tree r)]))

Note: match struct w/ (name params…)

slide-18
SLIDE 18

Define is-sorted

slide-19
SLIDE 19

Can match a list of x’s

(list x y z …) (1 2 3 4) x = 1 y = 2 z = ‘(3 4)

slide-20
SLIDE 20

Can match cons cells too…

(cons x y)

slide-21
SLIDE 21

Variants include things like match-let

slide-22
SLIDE 22

IO

slide-23
SLIDE 23

Racket has a “reader”

slide-24
SLIDE 24

(read)

slide-25
SLIDE 25

Racket “reads” the input one datum at a time

slide-26
SLIDE 26

> (read) (1 2 3) '(1 2 3) > (read) 1 2 3 1 > (read) 2 > (read) 3 >

slide-27
SLIDE 27

Read will “buffer” its input

slide-28
SLIDE 28
slide-29
SLIDE 29

(read-line)

slide-30
SLIDE 30

(open-input-file)

slide-31
SLIDE 31

Contracts

slide-32
SLIDE 32

(define (reverse-string s) (list->string (reverse (string->list s))))

slide-33
SLIDE 33

Write out the call and return type of this for yourself

slide-34
SLIDE 34

(define (factorial i) (cond [(= i 1) 1] [else (* (factorial (- i 1)) i)]))

slide-35
SLIDE 35

What are the call / return types?

slide-36
SLIDE 36

What is the pre / post condition?

slide-37
SLIDE 37

(define (gt0? x) (> x 0))

slide-38
SLIDE 38

(define/contract (factorial i) (-> gt0? gt0?) (cond [(= i 1) 1] [else (* (factorial (- i 1)) i)]))

slide-39
SLIDE 39

Now in tail form…

slide-40
SLIDE 40

(define (fac-tail i) (letrec ([h (lambda (i acc) (cond [(= i 0) acc] [else (h (- i 1) (* acc i))]))]) (h i 1)))

slide-41
SLIDE 41

Now, let’s say I want to say it’s equal to factorial…

slide-42
SLIDE 42

(define/contract (fac-tail i) (->i ([x (>=/c 0)]) [result (x) (lambda (result) (= (factorial x) result))]) (letrec ([h (lambda (i acc) (cond [(= i 0) acc] [else (h (- i 1) (* acc i))]))]) (h i 1)))

slide-43
SLIDE 43

(->i ([x (>=/c 0)]) [result (x) (lambda (result) (= (factorial x) result))])

slide-44
SLIDE 44

(define/contract (reverse-string s) (-> string? string?) (list->string (reverse (string->list s))))

slide-45
SLIDE 45

(define/contract (reverse-string s) (-> string? string?) (list->string (reverse (string->list s))))

slide-46
SLIDE 46

(<=/c 2)

slide-47
SLIDE 47

<=/c takes an argument x, returns a function f that takes an argument y, and f(y) = #t if x < = y

slide-48
SLIDE 48

<=/c takes an argument x, returns a function f that takes an argument y, and f(y) = #t if x < = y (Note: <=/c is also doing some bookeeping, but we won’t worry about that now.)

slide-49
SLIDE 49

Challenge: write <=/c

slide-50
SLIDE 50

Three stories

slide-51
SLIDE 51
slide-52
SLIDE 52

(define/contract (call-and-concat f s1 s2) (-> (-> string? string?) string? string? string?) (string-append (f s1) (f s2)))

(define (reverse-string s) (list->string (reverse (string->list s))))

slide-53
SLIDE 53

Scenario: you call call-and-concat with reverse

slide-54
SLIDE 54

Scenario: you call call-and-concat with reverse, 12, and “12"

slide-55
SLIDE 55

Now define

(define/contract (call-and-concat f s1 s2) (-> (-> string? string?) string? string? string?) (length (string-append (f s1) (f s2))))

slide-56
SLIDE 56

Now define

(define/contract (call-and-concat f s1 s2) (-> (-> string? string?) string? string? string?) (length (string-append (f s1) (f s2))))

What went wrong?

slide-57
SLIDE 57

Now define

(define/contract (call-and-concat f s1 s2) (-> (-> string? string?) string? string? string?) (length (string-append (f s1) (f s2))))

What went wrong? Who is to blame?