-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex4-structs.rkt
86 lines (65 loc) · 2.02 KB
/
ex4-structs.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#lang racket
;; Structs automatically create a set of procedures
; e.g. person? set-person-age?
;; SEE https://docs.racket-lang.org/reference/define-struct.html
(struct person
(name
[age #:mutable]))
(define p1 (person "Ada" 24))
(define p2 "Bob")
(person? p1)
(person? p2)
(set-person-age! p1 28)
(person-age p1)
;; Trees!
(struct node
([value #:mutable]))
(struct binary-node node
(left
right))
(define (leaf? n)
(and (node? n) (not (binary-node? n))))
; let's make a simple tree
(define a-tree (binary-node 2 (binary-node 3 (node 4) (node 2)) (node 1)))
(define (print-tree n)
(displayln (node-value n))
(unless (leaf? n)
(print-tree (binary-node-left n))
(print-tree (binary-node-right n))))
(print-tree a-tree)
;; ====== DISPLAY TREE NICELY ======
; feel free to ignore this code
(require pict)
(require pict/tree-layout)
(define (tree-show n)
(define (tree-show-helper n)
(cond [(leaf? n) (tree-layout #:pict (text (~a (node-value n))))]
[(binary-node? n) (tree-layout #:pict (text (~a (node-value n)))
(tree-show-helper (binary-node-left n))
(tree-show-helper (binary-node-right n)))]))
(naive-layered (tree-show-helper n)))
;; =================================
; displays the tree graphically
(tree-show a-tree)
; make a apply higher-order function for trees!
; applies the function f(x) to the value 'x' of
; each tree node.
#`(define (tree-apply f n)
(set-node-value! n (f (node-value n)))
(unless (leaf? n)
(begin
(tree-apply f (binary-node-left n))
(tree-apply f (binary-node-right n)))))
#`(print-tree a-tree)
(define (tree-apply f n)
(set-node-value! n (f (node-value n)))
(when (binary-node? n)
(begin
(tree-apply f (binary-node-left n))
(tree-apply f (binary-node-right n))))
n)
(tree-show
(tree-apply add1 a-tree))
(print-tree a-tree)
; Optional: Make a (tree-map f n) procedure that
; does not operate 'in place' like (tree-apply f n) does.