-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.foo
49 lines (37 loc) · 1013 Bytes
/
script.foo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-- Foo Lang
-- Script below containts valid code.
-- Identity function with polymorphic type
let identity x = x
-- Equivalent to expression below using lambda
let identity = (\x -> x)
-- a few polymorphic functions
-- Returns first argument
let fst a b = a
-- Returns second argument
let snd a b = b
-- Classic haskell function
let flip f x y = f y x
-- Functions returing the same output regardless of input
let constOne = fst 1
let constTrue = fst True
-- Absolute value of an integer
let abs x =
if (x < 0) then
(-1) * x
else
identity x
-- To create recursive functions add rec keyword after let.
let rec fib n =
if (n <= 0) then
0
else if (n == 1) then
1
else
(fib (n - 1)) + (fib (n - 2))
-- This is somewhat untrue definiton of a factorial function,
-- but let's not worry about gamma function and fancy math definitions :)
let rec factorial n =
if (n <= 1) then
1
else
n * (factorial (n - 1))