[FMFP] Interpreters

This commit is contained in:
2026-03-26 17:19:27 +01:00
parent bca4434c3c
commit e7f688c1b7
12 changed files with 224 additions and 81 deletions
+13 -7
View File
@@ -1,11 +1,17 @@
-- Declaring a function. Naming using lowerCamelCase
-- Arguments separated by whitespace
myFunc :: Int -> Int -> Int
-- Declaring a function. Naming using lowerCamelCase. Arguments separated by whitespace
-- We can omit the parenthesis in the type (they show what compiler does)
-- The below function uses guards (boolean checks)
myFunc :: Int -> (Int -> Int)
myFunc x y
| x > 0 = x + y
| x < 0 = -x + y
| otherwise = 0
-- On compile the above function is transformed like this:
-- TODO: Transform the template into correct version
myFuncXCompiled :: Int -> Int
myFuncXCompiled x = x
-- If the checks are pattern-based, use pattern matching. _ is wildcard (any value)
-- It is used when we don't need the variable. You can combine pattern-matching and guards
myOtherFunc :: Int -> Int -> Int
myOtherFunc 0 _ = 0
myOtherFunc x 0 = x
myOtherFunc x y
| x > 0 = x + y
| x < 0 = -x + y