diff --git a/moonbit-tour/tour/chapter1_basics/lesson8_control_flow/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson10_loop/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson8_control_flow/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson10_loop/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson8_control_flow/index.md b/moonbit-tour/tour/chapter1_basics/lesson10_loop/index.md similarity index 75% rename from moonbit-tour/tour/chapter1_basics/lesson8_control_flow/index.md rename to moonbit-tour/tour/chapter1_basics/lesson10_loop/index.md index 5572862c..36eea0b8 100644 --- a/moonbit-tour/tour/chapter1_basics/lesson8_control_flow/index.md +++ b/moonbit-tour/tour/chapter1_basics/lesson10_loop/index.md @@ -1,9 +1,8 @@ -# Control flow +# Loop -In this example, we use for loops, while loops, and an if-else expression to -iterate over an array. +In this example, we use for loops and while loops to iterate over an array. -## For loop +## For Loop Expression The for loop is analogous to a C-style for loop: @@ -18,11 +17,11 @@ The loop initializes the variables in the `init` part before it starts. When the In MoonBit, the for loop is more expressive than the C-style for loop. We will explain it in the following chapters. -## While loop and if-else expression +## While Loop Expression The while loop is also similar to the C-style while loop. It tests the condition before executing the loop body. If the condition is true, it executes the loop body and repeats the process until the condition is false. -MoonBit also supports both `continue` and `break` within the loop. +MoonBit also supports both `continue` and `break` statements within the loop. diff --git a/moonbit-tour/tour/chapter1_basics/lesson9_for_in_loop/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson11_for_in_loop/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson9_for_in_loop/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson11_for_in_loop/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson9_for_in_loop/index.md b/moonbit-tour/tour/chapter1_basics/lesson11_for_in_loop/index.md similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson9_for_in_loop/index.md rename to moonbit-tour/tour/chapter1_basics/lesson11_for_in_loop/index.md diff --git a/moonbit-tour/tour/chapter1_basics/lesson10_test/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson12_test/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson10_test/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson12_test/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson10_test/index.md b/moonbit-tour/tour/chapter1_basics/lesson12_test/index.md similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson10_test/index.md rename to moonbit-tour/tour/chapter1_basics/lesson12_test/index.md diff --git a/moonbit-tour/tour/chapter1_basics/lesson3_function/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson3_function/index.mbt index 2007599f..a3e8bbbd 100644 --- a/moonbit-tour/tour/chapter1_basics/lesson3_function/index.mbt +++ b/moonbit-tour/tour/chapter1_basics/lesson3_function/index.mbt @@ -1,5 +1,5 @@ fn add(a : Int, b : Int) -> Int { - a + b + return a + b } fn compute() -> Unit { diff --git a/moonbit-tour/tour/chapter1_basics/lesson4_block/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson4_block/index.mbt new file mode 100644 index 00000000..8a1e8b7b --- /dev/null +++ b/moonbit-tour/tour/chapter1_basics/lesson4_block/index.mbt @@ -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 +} + + diff --git a/moonbit-tour/tour/chapter1_basics/lesson4_block/index.md b/moonbit-tour/tour/chapter1_basics/lesson4_block/index.md new file mode 100644 index 00000000..e8df6683 --- /dev/null +++ b/moonbit-tour/tour/chapter1_basics/lesson4_block/index.md @@ -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. + + + + + diff --git a/moonbit-tour/tour/chapter1_basics/lesson4_array/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson5_array/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson4_array/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson5_array/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson4_array/index.md b/moonbit-tour/tour/chapter1_basics/lesson5_array/index.md similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson4_array/index.md rename to moonbit-tour/tour/chapter1_basics/lesson5_array/index.md diff --git a/moonbit-tour/tour/chapter1_basics/lesson5_string/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson6_string/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson5_string/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson6_string/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson5_string/index.md b/moonbit-tour/tour/chapter1_basics/lesson6_string/index.md similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson5_string/index.md rename to moonbit-tour/tour/chapter1_basics/lesson6_string/index.md diff --git a/moonbit-tour/tour/chapter1_basics/lesson6_tuple/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson7_tuple/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson6_tuple/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson7_tuple/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson6_tuple/index.md b/moonbit-tour/tour/chapter1_basics/lesson7_tuple/index.md similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson6_tuple/index.md rename to moonbit-tour/tour/chapter1_basics/lesson7_tuple/index.md diff --git a/moonbit-tour/tour/chapter1_basics/lesson7_map/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson8_map/index.mbt similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson7_map/index.mbt rename to moonbit-tour/tour/chapter1_basics/lesson8_map/index.mbt diff --git a/moonbit-tour/tour/chapter1_basics/lesson7_map/index.md b/moonbit-tour/tour/chapter1_basics/lesson8_map/index.md similarity index 100% rename from moonbit-tour/tour/chapter1_basics/lesson7_map/index.md rename to moonbit-tour/tour/chapter1_basics/lesson8_map/index.md diff --git a/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.mbt b/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.mbt new file mode 100644 index 00000000..0b8c09e7 --- /dev/null +++ b/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.mbt @@ -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" + } +} diff --git a/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.md b/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.md new file mode 100644 index 00000000..e23b94c2 --- /dev/null +++ b/moonbit-tour/tour/chapter1_basics/lesson9_if_else/index.md @@ -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`. + + + + +