Skip to content

Commit bafa5bc

Browse files
skylee03peter-jerry-ye
authored andcommitted
feat: update course3/lecture_en.md
1 parent 1dc54e4 commit bafa5bc

File tree

2 files changed

+622
-40
lines changed

2 files changed

+622
-40
lines changed

course3/course_en.md

+28-40
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,6 @@ style: |
1818
<!--
1919
```moonbit
2020
let pi = 3.1415
21-
22-
fn put(map: @map.Map[Int, Int64], num: Int, result: Int64) -> @map.Map[Int, Int64] {
23-
map.insert(num, result)
24-
}
25-
26-
fn get(map: @map.Map[Int, Int64], num: Int) -> Option[Int64] {
27-
map.lookup(num)
28-
}
29-
30-
fn make() -> @map.Map[Int, Int64] {
31-
@map.empty()
32-
}
33-
3421
```
3522
-->
3623

@@ -116,13 +103,13 @@ fn add_char(ch: Char, str: String) -> String {
116103
let moonbit: String = add_char(Char::from_int(109), "oonbit")
117104
```
118105

119-
$$\begin{align}
106+
$$\begin{aligned}
120107
& \texttt{add\_char(Char::from\_int(109), "oonbit")} \\
121108
\mapsto & \texttt{add\_char('m', "oonbit")} & \text{because \texttt{Char::from\_int(109)} $\mapsto$ \texttt{'m'}} \\
122109
\mapsto & \texttt{'m'.to\_string() + "oonbit"} & \text{replace the occurrences of the parameters} \\
123110
\mapsto & \texttt{"m" + "oonbit"} & \text{because \texttt{m.to\_string()} $\mapsto$ \texttt{"m"}} \\
124111
\mapsto & \texttt{"moonbit"} & \text{because \texttt{"m" + "oonbit"} $\mapsto$ \texttt{"moonbit"}}
125-
\end{align}$$
112+
\end{aligned}$$
126113

127114
---
128115

@@ -207,7 +194,7 @@ let x: Int = answer() // 42
207194
- Sometimes, we have data with the following characteristics:
208195
- The data is ordered.
209196
- The data can be duplicated.
210-
- The quantity of data is uncertain.
197+
- The data can vary in length.
211198
- For example:
212199
- Words in a sentence: [ `"Words"` `"in"` `"a"` `"sentence"` ]
213200
- DNA sequence: [`G` `A` `T` `T` `A` `C` `A`]
@@ -401,7 +388,7 @@ fn get(option_int: Option[Int64]) -> Int64 {
401388

402389
# Recursions
403390

404-
- Recursion is the process of breaking down a problem into smaller subproblems that are similar to the original problem but of a **smaller scale**.
391+
- Recursion is the process of breaking down a problem into smaller subproblems that are similar to the original problem but of a **reduced scale**.
405392
- A recursion should have one or more **base cases**.
406393
- In the definition of a function, it directly or indirectly calls itself.
407394

@@ -413,10 +400,10 @@ fn fib(n: Int) -> Int {
413400

414401
```moonbit
415402
fn even(n: Int) -> Bool {
416-
n != 1 && (n == 0 || odd(n - 1))
403+
n == 0 || odd(n - 1)
417404
}
418405
fn odd(n: Int) -> Bool {
419-
n != 0 && (n == 1 || even(n - 1))
406+
n == 1 || even(n - 1)
420407
}
421408
```
422409

@@ -582,7 +569,7 @@ A large number of duplicated subproblems was observed.
582569

583570
# Dynamic Programming
584571

585-
- Decompose the problem into smaller subproblems that are similar to the original problem but of a smaller scale.
572+
- Decompose the problem into smaller subproblems that are similar to the original problem but of a reduced scale.
586573
- Applicable to problems that have:
587574
- **Duplicated subproblems**: Dynamic programming solves each subproblem once and caches the result, avoiding redundant computations.
588575
- **Optimal substructure**: The global solution can be built from subproblems.
@@ -592,7 +579,7 @@ A large number of duplicated subproblems was observed.
592579

593580
---
594581

595-
# Dynamic Programming: Fibonacci Sequence
582+
# Solving Fibonacci Sequence with DP
596583

597584
- Dynamic programming is applicable to this problem.
598585
- Duplicated subproblems: Both `fib(n + 1)` and `fib(n + 2)` require `fib(n)`.
@@ -607,13 +594,14 @@ A large number of duplicated subproblems was observed.
607594
- We need a data structure whose average access efficiency is independent of the data size, and it should have the following interfaces:
608595

609596
```moonbit no-check
610-
fn empty() -> IntMap // Create
611-
fn insert(map: IntMap, num: Int, value: Int64) -> IntMap // Store
612-
fn lookup(map: IntMap, num: Int) -> Option[Int64] // Retrieve
597+
fn empty() -> IntMap // Create
598+
fn insert(self: IntMap, key: Int, value: Int64) -> IntMap // Store
599+
fn lookup(self: IntMap, key: Int) -> Option[Int64] // Retrieve
613600
```
614601

615602
- There are many siutable data structures, e.g., `@map.Map[Int, Int64]`.
616-
- Its implementation is not our main focus, and it can be repleced with `@vec.Vector[Option[Int64]]`.
603+
- Its implementation is not our main focus.
604+
- It can be repleced with other suitable data structures.
617605

618606
---
619607

@@ -626,16 +614,16 @@ fn lookup(map: IntMap, num: Int) -> Option[Int64] // Retrieve
626614
```moonbit expr
627615
fn fib1(num: Int) -> Int64 {
628616
fn aux(num: Int, map: @map.Map[Int, Int64]) -> (Int64, @map.Map[Int, Int64]) {
629-
match get(map, num) {
617+
match map.lookup(num) {
630618
Some(result) => (result, map)
631619
None => {
632620
let (result_1, map_1) = aux(num - 1, map)
633621
let (result_2, map_2) = aux(num - 2, map_1)
634-
(result_1 + result_2, put(map_2, num, result_1 + result_2))
622+
(result_1 + result_2, map_2.insert(num, result_1 + result_2))
635623
}
636624
}
637625
}
638-
let map = put(put(make(), 1, 1L), 2, 1L)
626+
let map = @map.empty().insert(1, 1L).insert(2, 1L)
639627
aux(num, map).0
640628
}
641629
```
@@ -650,15 +638,15 @@ To simplify the code, MoonBit supports mutable variables.
650638
```moonbit expr
651639
fn fib1_mut(num: Int) -> Int64 {
652640
// Declare a mutable variable with let mut
653-
let mut map = put(put(make(), 1, 1L), 2, 1L)
641+
let mut map = @map.empty().insert(1, 1L).insert(2, 1L)
654642
fn aux(num: Int) -> Int64 {
655-
match get(map, num) {
643+
match map.lookup(num) {
656644
Some(result) => result
657645
None => {
658646
let result_1 = aux(num - 1)
659647
let result_2 = aux(num - 2)
660648
// Update the binding with <variable> = <expression>
661-
map = put(map, num, result_1 + result_2)
649+
map = map.insert(num, result_1 + result_2)
662650
result_1 + result_2
663651
}
664652
}
@@ -676,12 +664,12 @@ Starting from the first one, calculate and store the subsequent items sequential
676664
```moonbit expr
677665
fn fib2(num: Int) -> Int64 {
678666
fn aux(n: Int, map: @map.Map[Int, Int64]) -> Int64 {
679-
let result = get_or_else(get(map, n - 1), 1L) +
680-
get_or_else(get(map, n - 2), 1L)
681-
if n == num { result }
682-
else { aux(n + 1, put(map, n, result)) }
667+
let result = get_or_else(map.lookup(n - 1), 1L) +
668+
get_or_else(map.lookup(n - 2), 1L)
669+
if n == num { result }
670+
else { aux(n + 1, map.insert(n, result)) }
683671
}
684-
let map = put(put(make(), 0, 0L), 1, 1L)
672+
let map = @map.empty().insert(0, 0L).insert(1, 1L)
685673
aux(1, map)
686674
}
687675
```
@@ -715,9 +703,9 @@ fn fib2(num : Int) -> Int64 {
715703
- Data structure: lists and pattern matching on lists
716704
- Algorithm: recursions and dynamic programming
717705
- Extended reading:
718-
- _Software Foundations, Volume 1: Logical Foundations_
706+
- _**Software Foundations, Volume 1: Logical Foundations**_
719707
Basics, Induction & Lists
720-
- _Programming Language Foundations in Agda_
708+
- _**Programming Language Foundations in Agda**_
721709
Naturals, Induction & Relations
722-
- _Introduction to Algorithms_
723-
Chapter 15 (3e) / Chapter 14 (4e): Dynamic Programming
710+
- _**Introduction to Algorithms**_
711+
Chapter 15 (3e) / Chapter 14 (4e) - Dynamic Programming

0 commit comments

Comments
 (0)