|
4 | 4 | ```moonbit
|
5 | 5 | let pi = 3.1415
|
6 | 6 |
|
7 |
| -fn put(map: @map.Map[Int, Int64], num: Int, result: Int64) -> @map.Map[Int, Int64] { |
| 7 | +fn put(map: @immut/sorted_map.Map[Int, Int64], num: Int, result: Int64) -> @immut/sorted_map.Map[Int, Int64] { |
8 | 8 | map.insert(num, result)
|
9 | 9 | }
|
10 | 10 |
|
11 |
| -fn get(map: @map.Map[Int, Int64], num: Int) -> Option[Int64] { |
| 11 | +fn get(map: @immut/sorted_map.Map[Int, Int64], num: Int) -> Option[Int64] { |
12 | 12 | map.lookup(num)
|
13 | 13 | }
|
14 | 14 |
|
15 |
| -fn make() -> @map.Map[Int, Int64] { |
16 |
| - @map.empty() |
| 15 | +fn make() -> @immut/sorted_map.Map[Int, Int64] { |
| 16 | + @immut/sorted_map.empty() |
17 | 17 | }
|
18 | 18 |
|
19 | 19 | ```
|
@@ -264,7 +264,7 @@ The following examples help deepen our understanding of lists.
|
264 | 264 | - `Cons(1, 2)`: `2` itself is not a list.
|
265 | 265 | - `Cons(1, Cons(Nil, Nil))`: Items are of different types.
|
266 | 266 |
|
267 |
| -Like `Option[T]`, the list type `List[T]` is also genereic. |
| 267 | +Like `Option[T]`, the list type `List[T]` is also generic. |
268 | 268 |
|
269 | 269 | - A list of integers is of type `List[Int]`.
|
270 | 270 | - A list of strings is of type `List[String]`.
|
@@ -555,13 +555,13 @@ fn get(map: IntMap, num: Int) -> Option[Int64] // Retrieve
|
555 | 555 |
|
556 | 556 | In other words, we should be able to perform the following operations using an `IntMap`: create an empty map, insert a key-value pair into it, and look up the value corresponding to a given key.
|
557 | 557 |
|
558 |
| -Thanks to our paradigm of modular programming, we only need to care about the interfaces rather than the specific implementation. Therefore, there are many siutable data structures in MoonBit's standard library. In this example, we will use `@map.Map[Int, Int64]`, but we can easily replace it with another data structure, as long as it implements the interfaces we need. |
| 558 | +Thanks to our paradigm of modular programming, we only need to care about the interfaces rather than the specific implementation. Therefore, there are many suitable data structures in MoonBit's standard library. In this example, we will use `@immut/sorted_map.Map[Int, Int64]`, but we can easily replace it with another data structure, as long as it implements the interfaces we need. |
559 | 559 |
|
560 | 560 | In the top-down implementation, before each computation, we first check if our desired result has been cached: if it does, we can simply use the result; if it doesn't, we calculate the result and store it in the data structure.
|
561 | 561 |
|
562 | 562 | ```moonbit expr
|
563 | 563 | fn fib1(num: Int) -> Int64 {
|
564 |
| - fn aux(num: Int, map: @map.Map[Int, Int64]) -> (Int64, @map.Map[Int, Int64]) { |
| 564 | + fn aux(num: Int, map: @immut/sorted_map.Map[Int, Int64]) -> (Int64, @immut/sorted_map.Map[Int, Int64]) { |
565 | 565 | match get(map, num) {
|
566 | 566 | Some(result) => (result, map)
|
567 | 567 | None => {
|
@@ -608,7 +608,7 @@ In the bottom-up implementation, we typically start from the smallest subproblem
|
608 | 608 |
|
609 | 609 | ```moonbit expr
|
610 | 610 | fn fib2(num: Int) -> Int64 {
|
611 |
| - fn aux(n: Int, map: @map.Map[Int, Int64]) -> Int64 { |
| 611 | + fn aux(n: Int, map: @immut/sorted_map.Map[Int, Int64]) -> Int64 { |
612 | 612 | let result = get_or_else(get(map, n - 1), 1L) +
|
613 | 613 | get_or_else(get(map, n - 2), 1L)
|
614 | 614 | if n == num { result }
|
|
0 commit comments