-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise3.29.kt
41 lines (34 loc) · 1.1 KB
/
exercise3.29.kt
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
package chapter3
import chapter3.Tree.Leaf
import chapter3.Tree.Branch
import kotlin.math.max
fun <A, B> fold(tree: Tree<A>, l: (A) -> B, b: (B, B) -> B): B =
when (tree) {
is Leaf -> l(tree.a)
is Branch -> b(fold(tree.left, l, b), fold(tree.right, l, b))
}
fun <A> size2(tree: Tree<A>): Int =
fold(tree, { 1 }) { left, right -> 1 + left + right }
fun maximum2(tree: Tree<Int>): Int =
fold(tree, { max(it, Int.MIN_VALUE) }) { left, right -> max(left, right) }
fun <A> depth2(tree: Tree<A>): Int =
fold(tree, { 1 }) { left, right -> max(left + 1, right + 1) }
fun <A, B> map2(tree: Tree<A>, f: (A) -> B): Tree<B> =
fold(tree, { treeOf(f(it)) }) { left, right -> Branch(left, right) }
fun main() {
val tree = Branch(
Leaf(5),
Branch(
Branch(Leaf(10), Leaf(1)),
Leaf(2)
)
)
println(size2(tree))
println(maximum2(tree))
println(depth2(tree))
println(map2(tree) { it + 10 })
// 7
// 10
// 4
// Branch(left=Leaf(a=15), right=Branch(left=Branch(left=Leaf(a=20), right=Leaf(a=11)), right=Leaf(a=12)))
}