From a99eebca28c3046f5c50a0a65dbb9a2e4b047637 Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sat, 23 Aug 2025 10:05:28 +0300 Subject: doc: added exercise on Collatz conjecture --- TUTORIAL.md | 20 ++++++++++++++++---- solutions/collatz.slip | 6 ++++++ 2 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 solutions/collatz.slip diff --git a/TUTORIAL.md b/TUTORIAL.md index 30e7d62..ba6b8a1 100644 --- a/TUTORIAL.md +++ b/TUTORIAL.md @@ -424,7 +424,19 @@ language. The solutions can be found in the `solutions` folder under the project root. -Exercise 1: Running code +Exercise 1: Collatz conjecture step +----------------------------------- + +Given an integer `x`, compute the next step of the sequence +famous from the Collatz conjecture. In other words, if `x` +is even, return half of `x`, and if `x` is odd, return +`3x + 1`. + +This should make you familiar with arithmetic and +conditional logic. + + +Exercise 2: Running code ------------------------ Write a function `cond-add-inv` in a file called `a.slip`. @@ -448,7 +460,7 @@ may want to try out the `--babysteps` feature (see `myslip --help`). -Exercise 2: Fibonacci sequence +Exercise 3: Fibonacci sequence ------------------------------ Write a function that calculates the n:th fibonacci number, @@ -468,7 +480,7 @@ n saved ``` -Exercise 3: Generic functions – the identity +Exercise 4: Generic functions – the identity -------------------------------------------- Write the identity function for any type. @@ -477,7 +489,7 @@ This should make you familiar with typing function definitions that contain generics. -Exercise 4: Map left / right (integers) +Exercise 5: Map left / right (integers) --------------------------------------- Write functions `map-left` and `map-right` that take a diff --git a/solutions/collatz.slip b/solutions/collatz.slip new file mode 100644 index 0000000..e561b17 --- /dev/null +++ b/solutions/collatz.slip @@ -0,0 +1,6 @@ + +(let x 7) +(if (= (* (/ x 2) 2) x) + (/ x 2) + (+ (* 3 x) 1)) + -- cgit v1.2.3