aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-18 23:39:47 +0300
committerJoel Kronqvist <joel.kronqvist@iki.fi>2025-08-18 23:39:47 +0300
commitd4d6e972650370d529363f7db3946e58bdbd7bca (patch)
treeafea437941d0baf701f768b246ecf0af5e0b427a
parentfffe2d4405885194c5f584755ed4f02de1027558 (diff)
downloadmyslip-d4d6e972650370d529363f7db3946e58bdbd7bca.tar.gz
myslip-d4d6e972650370d529363f7db3946e58bdbd7bca.zip
doc: added exercise 1
-rw-r--r--TUTORIAL.md54
-rw-r--r--solutions/fibonacci.slip13
-rw-r--r--src/type/check.rs2
3 files changed, 56 insertions, 13 deletions
diff --git a/TUTORIAL.md b/TUTORIAL.md
index 2f4edf8..0f6a21a 100644
--- a/TUTORIAL.md
+++ b/TUTORIAL.md
@@ -146,7 +146,7 @@ others.
++ saved
> (++ 1)
2 : Int
-> (let between (fn (a b c) (Int Int Int) bool (and (< b c) (> b a))))
+> (let between (fn (a b c) (Int Int Int) Bool (and (< b c) (> b a))))
between saved
> (between 1 2 3)
true : Bool
@@ -264,22 +264,13 @@ true : Bool
Pattern matching can be done on vectors too.
Just remember that the rest pattern `..r` needs to be at
-the end of the pattern — not at the end of a subpattern.
+the end of the whole pattern.
```console
> (let myvec (vector 1 2 3 4 5))
myvec saved
> (case myvec ((head ..tail) head) (_ 0))
1 : Int
```
-Here's a failing example of trying to put a rest pattern at
-the end of a subpattern:
-```console
-> (case (quote myvec false) (((head ..tail) false) head) (_ 0))
-Type error: unbound generic type 'T' in 'T'
-```
-Try extracting the vector first through one pattern and
-then pattern matching on the extracted vector for your
-desired behaviour.
General recursion
@@ -305,8 +296,47 @@ The factorial function could be implemented as such:
))
```
+Printing
+--------
+
+This is quite a useless feature as the repl already
+echoes all results. But if you fancy, you can print
+anything using the `print` operator, which is
+behaviorally equivalent to Nil as long as its argument
+is valid myslip. For example, with the standard library
+enabled:
+```console
+> (let x 1)
+x saved
+> (print 1)
+() : Nil
+> (print sum)
+(fn vec (Vector (Int ...)) Int (case vec ((h ..t) + h ((fix (fn sum' ((Vector (Int ...)) -> Int) ((Vector (Int ...)) -> Int) (fn vec (Vector (Int ...)) Int (case vec ((h ..t) + h (sum' t)) (_ 0))))) t)) (_ 0)))
+() : Nil
+```
+
Exercises
=========
-TODO
+These exercises are meant to make you familiar with the
+language. The solutions can be found in the `solutions`
+folder under the project root.
+
+Exercise 1: Fibonacci sequence
+------------------------------
+
+Write a function that calculates the n:th fibonacci number.
+
+This should make you familiar with function definitions and
+fixed point recursion.
+
+Example of desired behaviour:
+```console
+> (fibonacci 1)
+0 : Int
+> (let n 10)
+n saved
+> (fibonacci n)
+34 : Int
+```
diff --git a/solutions/fibonacci.slip b/solutions/fibonacci.slip
new file mode 100644
index 0000000..29d0437
--- /dev/null
+++ b/solutions/fibonacci.slip
@@ -0,0 +1,13 @@
+(let fibonacci (fix
+ (fn fib' (Int -> Int) (Int -> Int)
+ (fn n Int Int
+ (case n
+ (1 0)
+ (2 1)
+ (n
+ (+ (fib' (-- n)) (fib' (- n 2)))
+ )
+ )
+ )
+ )
+))
diff --git a/src/type/check.rs b/src/type/check.rs
index 6d54570..c0f4b1f 100644
--- a/src/type/check.rs
+++ b/src/type/check.rs
@@ -261,7 +261,7 @@ impl SExp {
List(vec![VecType, vecof(vt("T"))])
)),
Atom(Let) => Ok(LetType),
- Atom(Print) => Ok(arr(vt("_"), List(vec![]))),
+ Atom(Print) => Ok(arr(vt("_"), NilType)),
Atom(Ty(_)) => Ok(TypeLit),
Atom(Fun) => Err(FunAsAtom),
Atom(Fix) => Ok(arr(arr(vt("T"), vt("T")), vt("T"))),