diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2021-10-26 23:57:25 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2021-10-26 23:57:25 -0700 |
commit | 20cca772b85661b607af760d0b77ef603b5ce222 (patch) | |
tree | 1ae6e2a78b3979d7d24a0875a9dd10779a8e852f /tutorial | |
parent | a3f73278b198d0366c590ec8bd7fb8cad5c2931d (diff) | |
download | mu-20cca772b85661b607af760d0b77ef603b5ce222.tar.gz |
task: conditional execution
Diffstat (limited to 'tutorial')
-rw-r--r-- | tutorial/index.md | 31 | ||||
-rw-r--r-- | tutorial/task11-solution.mu | 22 | ||||
-rw-r--r-- | tutorial/task11.mu | 14 |
3 files changed, 67 insertions, 0 deletions
diff --git a/tutorial/index.md b/tutorial/index.md index 62577a5f..a147b9bc 100644 --- a/tutorial/index.md +++ b/tutorial/index.md @@ -421,3 +421,34 @@ answer. Start from the first, and bump down if you need a hint. * tutorial/task10-hint1.mu * tutorial/task10-hint2.mu * tutorial/task10-hint3.mu + +## Task 11: conditionally executing statements + +Here's a fragment of Mu code: + +``` +{ + compare x, 0 + break-if-< + x <- copy 0 +} +``` + +The combination of `compare` and `break` results in the variable `x` being +assigned 0 _if and only if_ its value was less than 0 at the beginning. The +`break` family of instructions is used to jump to the end of an enclosing `{}` +block, skipping all intervening instructions. + +To prepare for this task, read the sections in the Mu reference on +[`compare`](https://github.com/akkartik/mu/blob/main/mu.md#comparing-values) +and [branches](https://github.com/akkartik/mu/blob/main/mu.md#branches). + +Now make the tests pass in `tutorial/task11.mu`. The goal is to implement our +colloquial understanding of the “difference” between two numbers. +In lay English, we say the difference between the first-place and third-place +runner in a race is two places. This answer doesn't depend on the order in +which we mention the runners; the difference between third and first is also +two. + +The section on [integer arithmetic](https://github.com/akkartik/mu/blob/main/mu.md#integer-arithmetic) +is again worth referring to when working on this task. diff --git a/tutorial/task11-solution.mu b/tutorial/task11-solution.mu new file mode 100644 index 00000000..85140470 --- /dev/null +++ b/tutorial/task11-solution.mu @@ -0,0 +1,22 @@ +fn difference a: int, b: int -> _/eax: int { + var result/eax: int <- copy a + result <- subtract b + compare result, 0 + { + break-if->= + result <- negate + } + return result +} + +fn test-difference { + var result/eax: int <- difference 5, 3 + check-ints-equal result, 2, "F - difference works" + result <- difference 3, 5 + check-ints-equal result, 2, "F - difference is always positive" + result <- difference 6, 6 + check-ints-equal result, 0, "F - difference can be 0" +} + +fn main screen: (addr screen) { +} diff --git a/tutorial/task11.mu b/tutorial/task11.mu new file mode 100644 index 00000000..0b9796e7 --- /dev/null +++ b/tutorial/task11.mu @@ -0,0 +1,14 @@ +fn difference a: int, b: int -> _/eax: int { +} + +fn test-difference { + var result/eax: int <- difference 5, 3 + check-ints-equal result, 2, "F - difference works" + result <- difference 3, 5 + check-ints-equal result, 2, "F - difference is always positive" + result <- difference 6, 6 + check-ints-equal result, 0, "F - difference can be 0" +} + +fn main screen: (addr screen) { +} |