Skip to content

Commit

Permalink
update sample code for expression reduction
Browse files Browse the repository at this point in the history
  • Loading branch information
skylee03 committed Apr 3, 2024
1 parent 6a35a7d commit 297458f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 31 deletions.
37 changes: 20 additions & 17 deletions course2/course_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,8 @@ Top-level definitions have a global scope, while local definitions have a local
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
let y: Int = 10
let value = {
```moonbit
let value: Int = {
let x = 1
let tmp = x * 2
let another_tmp = {
Expand All @@ -379,6 +378,8 @@ let value = {
}
tmp + another_tmp + y
}
let y: Int = 10
```

---
Expand All @@ -390,9 +391,8 @@ let value = {
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
let y: Int = 10
let value = {
```moonbit no-check
let value: Int = {
let x = 1
let tmp = 1 * 2 // Replace x
let another_tmp = {
Expand All @@ -402,6 +402,8 @@ let value = {
}
tmp + another_tmp + 10 // Replace y
}
let y: Int = 10
```

---
Expand All @@ -413,9 +415,8 @@ let value = {
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
// Omit the variable binding of y
let value = {
```moonbit no-check
let value: Int = {
// Omit the variable binding of x
let tmp = 2 // Reduce the expression on the right-hand side
let another_tmp = {
Expand All @@ -425,6 +426,8 @@ let value = {
}
tmp + another_tmp + 10
}
// Omit the variable binding of y
```

---
Expand All @@ -436,8 +439,8 @@ let value = {
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
let value = {
```moonbit no-check
let value: Int = {
let tmp = 2
let another_tmp = {
Expand All @@ -458,8 +461,8 @@ let value = {
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
let value = {
```moonbit no-check
let value: Int = {
let tmp = 2
let another_tmp = 3 // Reduce the expression on the right-hand side
Expand All @@ -476,8 +479,8 @@ let value = {
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
let value = {
```moonbit no-check
let value: Int = {
let tmp = 2
let another_tmp = 3 // Reduce the expression on the right-hand side
Expand All @@ -494,8 +497,8 @@ let value = {
- Omit the variable binding part.
- Reduce the remaining expressions.

```moonbit expr
let value = 15
```moonbit no-check
let value: Int = 15
```

---
Expand Down
30 changes: 16 additions & 14 deletions course2/lecture_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,8 @@ Expression reduction can be broken down into the following steps:

Take the following code snippet as an example:

```moonbit expr
let y: Int = 10
let value = {
```moonbit
let value: Int = {
let x = 1
let tmp = x * 2
let another_tmp = {
Expand All @@ -273,13 +272,14 @@ let value = {
}
tmp + another_tmp + y
}
let y: Int = 10
```

First, we can replace all occurrences of `x` and `y` with their values, thereby omitting their variable bindings.

```moonbit expr
// Omit the variable binding of y
let value = {
```moonbit no-check
let value: Int = {
// Omit the variable binding of x
let tmp = 1 * 2 // Replace x
let another_tmp = {
Expand All @@ -289,12 +289,14 @@ let value = {
}
tmp + another_tmp + 10 // Replace y
}
// Omit the variable binding of y
```

Then, we can reduce the expression for two variable bindings of `tmp`, and replace the occurrences of `tmp` in the expression block of the variable binding of `another_tmp`.

```moonbit expr
let value = {
```moonbit no-check
let value: Int = {
let tmp = 2 // Reduce the expression on the right-hand side
let another_tmp = {
let tmp = 3 // Reduce the expression on the right-hand side
Expand All @@ -307,8 +309,8 @@ let value = {

After that, we can now compute the value of `another_tmp`, which is determined by the last expression in the expression block.

```moonbit expr
let value = {
```moonbit no-check
let value: Int = {
let tmp = 2
let another_tmp = 3 // Reduce the expression on the right-hand side
Expand All @@ -318,8 +320,8 @@ let value = {

Thus, the remaining occurrences of identifiers in the expression block of `value` can also be replaced with their values.

```moonbit expr
let value = {
```moonbit no-check
let value: Int = {
let tmp = 2
let another_tmp = 3
Expand All @@ -329,8 +331,8 @@ let value = {

Fanally, we get the value of `value`.

```moonbit expr
let value = 15
```moonbit no-check
let value: Int = 15
```

#### Conditional Expression
Expand Down

0 comments on commit 297458f

Please sign in to comment.