From 20cca772b85661b607af760d0b77ef603b5ce222 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 26 Oct 2021 23:57:25 -0700 Subject: task: conditional execution --- tutorial/index.md | 31 +++++++++++++++++++++++++++++++ tutorial/task11-solution.mu | 22 ++++++++++++++++++++++ tutorial/task11.mu | 14 ++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 tutorial/task11-solution.mu create mode 100644 tutorial/task11.mu (limited to 'tutorial') 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) { +} -- cgit 1.4.1-2-gfad0