Skip to content

Commit b39a8e0

Browse files
committed
Remove last of quasiquote style in OCaml to Racket notes.
1 parent 4a15bae commit b39a8e0

2 files changed

Lines changed: 34 additions & 27 deletions

File tree

langs/intro/bt.rkt

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,36 @@
55
(require rackunit))
66

77
;; type Bt =
8-
;; | `leaf
9-
;; | `(node ,Integer ,Bt ,Bt)
8+
;; | (leaf)
9+
;; | (node Integer Bt Bt)
10+
(struct leaf () #:prefab)
11+
(struct node (v l r) #:prefab)
1012

1113
;; Bt -> Boolean
1214
;; Is the binary tree empty?
1315
(define (bt-empty? bt)
1416
(match bt
15-
['leaf #t]
16-
[(cons 'node _) #f]))
17+
[(leaf) #t]
18+
[_ #f]))
1719

1820
(module+ test
19-
(check-equal? (bt-empty? 'leaf) #t)
20-
(check-equal? (bt-empty? '(node 3
21-
(node 7 leaf leaf)
22-
(node 9 leaf leaf)))
21+
(check-equal? (bt-empty? (leaf)) #t)
22+
(check-equal? (bt-empty? (node 3
23+
(node 7 (leaf) (leaf))
24+
(node 9 (leaf) (leaf))))
2325
#f))
2426

2527
;; Bt -> Natural
2628
;; Compute the height of a binary tree
2729
(define (bt-height bt)
2830
(match bt
29-
[`leaf 0]
30-
[`(node ,_ ,left ,right)
31+
[(leaf) 0]
32+
[(node _ left right)
3133
(+ 1 (max (bt-height left)
3234
(bt-height right)))]))
3335

3436
(module+ test
35-
(check-equal? (bt-height 'leaf) 0)
36-
(check-equal? (bt-height '(node 3 leaf leaf)) 1)
37-
(check-equal? (bt-height '(node 2 leaf (node 1 leaf leaf)))
37+
(check-equal? (bt-height (leaf)) 0)
38+
(check-equal? (bt-height (node 3 (leaf) (leaf))) 1)
39+
(check-equal? (bt-height (node 2 (leaf) (node 1 (leaf) (leaf))))
3840
2))

www/notes/1/ocaml-to-racket.scrbl

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1168,11 +1168,13 @@ also write modules without saving them in a file. For example:
11681168

11691169
@ex[
11701170
(module bt racket
1171-
(provide bt-height)
1171+
(provide (all-defined-out))
1172+
(struct leaf () #:prefab)
1173+
(struct node (v l r) #:prefab)
11721174
(define (bt-height bt)
11731175
(match bt
1174-
[`leaf 0]
1175-
[`(node ,_ ,left ,right)
1176+
[(leaf) 0]
1177+
[(node _ left right)
11761178
(+ 1 (max (bt-height left)
11771179
(bt-height right)))])))
11781180
]
@@ -1185,7 +1187,7 @@ provided values:
11851187

11861188
@ex[
11871189
(require 'bt)
1188-
(bt-height 'leaf)
1190+
(bt-height (leaf))
11891191
]
11901192

11911193
We could have also used the @tt{#lang racket} shorthand for
@@ -1214,29 +1216,32 @@ provide everything:
12141216
(module+ test
12151217
(require rackunit))
12161218

1219+
(struct leaf () #:prefab)
1220+
(struct node (v l r) #:prefab)
1221+
12171222
(define (bt-empty? bt)
12181223
(match bt
1219-
['leaf #t]
1220-
[(cons 'node _) #f]))
1224+
[(leaf) #t]
1225+
[_ #f]))
12211226

12221227
(module+ test
1223-
(check-equal? (bt-empty? 'leaf) #t)
1224-
(check-equal? (bt-empty? '(node 3
1225-
(node 7 leaf leaf)
1226-
(node 9 leaf leaf)))
1228+
(check-equal? (bt-empty? (leaf)) #t)
1229+
(check-equal? (bt-empty? (node 3
1230+
(node 7 (leaf) (leaf))
1231+
(node 9 (leaf) (leaf))))
12271232
#f))
12281233

12291234
(define (bt-height bt)
12301235
(match bt
1231-
[`leaf 0]
1232-
[`(node ,_ ,left ,right)
1236+
[(leaf) 0]
1237+
[(node _ left right)
12331238
(+ 1 (max (bt-height left)
12341239
(bt-height right)))]))
12351240

12361241
(module+ test
1237-
(check-equal? (bt-height 'leaf) 0)
1242+
(check-equal? (bt-height (leaf)) 0)
12381243
(code:comment "intentionally wrong test:")
1239-
(check-equal? (bt-height '(node 3 leaf leaf)) 2)))
1244+
(check-equal? (bt-height (node 3 (leaf) (leaf))) 2)))
12401245
]
12411246

12421247
Requiring this module with make @racket[bt-height], but @emph{it will not run the tests}:

0 commit comments

Comments
 (0)