-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tour: add more detail about block and if
- Loading branch information
Showing
19 changed files
with
110 additions
and
7 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
fn add(a : Int, b : Int) -> Int { | ||
a + b | ||
return a + b | ||
} | ||
|
||
fn compute() -> Unit { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
fn main { | ||
let a = 100 | ||
|
||
{ | ||
let mut a = 0 | ||
println("checkpoint 1") | ||
a = a + 1 | ||
} | ||
|
||
println("checkpoint 2") | ||
println(f()) | ||
} | ||
|
||
fn f() -> Int { | ||
let b = 3.14 | ||
|
||
let result = { | ||
let b = 1 | ||
println("checkpoint 3") | ||
b + 5 | ||
} | ||
|
||
result // same as `return result` here | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Block and Statements | ||
|
||
A block is an expression contains sequence of statements and an optional | ||
expression. | ||
|
||
``` | ||
{ | ||
statement1 | ||
statement2 | ||
expression | ||
} | ||
``` | ||
|
||
For example, the code above will execute `statement1`, `statement2` | ||
and evaluate `expression` in order. The evaluation result of the block is the | ||
evaluation result of the expression. If the last one expression is omitted, | ||
the block will result in `()`, which type is `Unit`. | ||
|
||
|
||
A statement can be a variable declaration, variable assignment, or any | ||
expression which type is `Unit`. | ||
|
||
|
||
A block is also associated with a namespace scope. In the `main` clause, the variable `a` declared in the inner block will shadow the outer `a`. It is only visible within the inner block. | ||
|
||
|
||
|
||
|
||
|
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
35 changes: 35 additions & 0 deletions
35
moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.mbt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
fn fib(x : Int) -> Int { | ||
if x < 2 { | ||
x | ||
} else { | ||
fib(x - 1) + fib(x - 2) | ||
} | ||
} | ||
|
||
fn main { | ||
if 5 > 1 { | ||
println("5 is greater than 1") | ||
} | ||
println(fib(5)) | ||
println(weekday(3)) | ||
} | ||
|
||
fn weekday(x : Int) -> String { | ||
if x == 1 { | ||
"Monday" | ||
} else if x == 2 { | ||
"Tuesday" | ||
} else if x == 3 { | ||
"Wednesday" | ||
} else if x == 4 { | ||
"Thursday" | ||
} else if x == 5 { | ||
"Friday" | ||
} else if x == 6 { | ||
"Saturday" | ||
} else if x == 7 { | ||
"Sunday" | ||
} else { | ||
"Invalid day" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# If expression | ||
|
||
An if expression is a conditional control flow expression that has a result value. | ||
|
||
In an if expression, each branch must have the same type. If the condition is true, it returns the result value of the first branch. Otherwise, it returns the result value of the second branch. | ||
|
||
The `else` part is optional. If it is omitted, the type of the whole if expression will be `Unit`. | ||
|
||
Nested if expressions in the else part can be shortened by using `else if`. | ||
|
||
|
||
|
||
|
||
|