aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-23 10:05:28 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-23 10:05:28 +0300
commita99eebca28c3046f5c50a0a65dbb9a2e4b047637 (patch)
tree2c66e4dd2b2f95ae87296999b488b51523de43a7
parent71fdb64eb4784f259bb6651f5039d2c54e2d01e8 (diff)
downloadmyslip-a99eebca28c3046f5c50a0a65dbb9a2e4b047637.tar.gz
myslip-a99eebca28c3046f5c50a0a65dbb9a2e4b047637.zip
doc: added exercise on Collatz conjecture
-rw-r--r--TUTORIAL.md20
-rw-r--r--solutions/collatz.slip6
2 files changed, 22 insertions, 4 deletions
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))
+