mirror of
https://github.com/janishutz/eth-summaries.git
synced 2026-04-28 16:19:23 +02:00
[FMFP] Interpreters
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user