From d4d6e972650370d529363f7db3946e58bdbd7bca Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Mon, 18 Aug 2025 23:39:47 +0300 Subject: doc: added exercise 1 --- TUTORIAL.md | 54 +++++++++++++++++++++++++++++++++++++----------- solutions/fibonacci.slip | 13 ++++++++++++ src/type/check.rs | 2 +- 3 files changed, 56 insertions(+), 13 deletions(-) create mode 100644 solutions/fibonacci.slip 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"))), -- cgit v1.2.3