Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update lecture&course of course7 #1

Merged
merged 19 commits into from
Mar 29, 2024
59 changes: 54 additions & 5 deletions course7/course_en.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Modern Programming Thoughts: Imperative Programming
# Modern Programming Ideology: Imperative Programming

DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved
## Revisiting Functional Programming

Expand Down Expand Up @@ -42,14 +42,34 @@ fn init {

Multiple identifiers pointing to the same mutable data structure can be considered aliases, which need to be handled carefully.
DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved

```moonbit
fn alter(a: Ref[Int], b: Ref[Int]) {
a.val = 10
b.val = 20
}

DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved
fn init {
let x: Ref[Int] = { val : 1 }
alter(x, x)
println(x.val.to_string()) // x.val will be changed twice
}
```

## Debugger

- Moon Rabbit's debugger allows us to see real-time running data during operation and better understand the running process

![](../pics/debugger.png)

## Loops

We can define loops using variables in MoonBit. A loop includes defining the loop variable and its initial value, checking whether to continue the loop, and iterating the variable.

```moonbit
let mut i = 0
while i < 2, i = i + 1 {
while i < 2 {
println("Output")
i = i + 1
DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved
} // Repeat output 2 times
```

Expand All @@ -62,7 +82,9 @@ Loops and recursion are equivalent. A loop can be written in a recursive form. T
```moonbit
// Loop form
fn fib_mut(n: Int) -> Int {
let mut a = 0; let mut b = 1; let mut i = 0
let mut a = 0;
let mut b = 1;
let mut i = 0
while i < n, i = i + 1 {
DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved
let t = a + b
a = b; b = t
Expand All @@ -85,17 +107,44 @@ Within a loop, we can use `break` to exit the loop prematurely and `continue` to
```moonbit
fn print_first_3() {
let mut i = 0
while i < 10, i = i + 1 {
while i < 10 {
if i == 3 {
break // Skip from 3 onwards
} else {
println(i.to_string())
}
i = i + 1
}
}
```

the excepted output is

```
0
1
2
```

if we change `break` to `continue`

```moonbit
fn print_first_3() {
let mut i = 0
while i < 10 {
if i == 3 {
continue // Skip from 3 onwards
} else {
println(i.to_string())
}
i = i + 1
}
}
```
DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved

## MoonBit Checks
the program will go into infinite loops, so there will be no output.

## MoonBit Check
DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved

MoonBit checks whether a variable is modified, which can help avoid mistakes like forgetting to add an iteration condition in a loop. It also checks if the function's return value type matches the declared type, preventing incorrect type declarations.

DawnMagnet marked this conversation as resolved.
Show resolved Hide resolved
Expand Down