roza/tests/acceptances/fun.roza

115 lines
1.3 KiB
Plaintext

# FUNCTIONS
# =========
fun double(n)
return n * 2
end
assert double(4) == 8
assert 8 == double(4)
assert (double(4) == 10) == false
assert (9 == double(4)) == false
assert double(double(double(3))) == 24
fun a()
return 45
end
assert a() == 45
fun b()
return 27
return 32
end
assert b() == 27
fun max(m, n)
if m > n then
return m
end
return n
end
assert max(7, 3) == 7
assert max(7, 9) == 9
# RECURSIVITY
# ===========
fun fac(n)
if n == 0 then
return 1
end
return n * fac(n - 1)
end
assert fac(5) == 120
fun fib(n)
if n == 0 or n == 1 then
return 1
end
return fib(n - 2) + fib(n - 1)
end
assert fib(6) == 13
# HIGH ORDER FUNCTIONS
# ====================
fun fi(n)
return n + 1
end
fun fd(n)
return n * 2
end
fun twice(f, x)
return f(f(x))
end
assert twice(fi, 27) == 29
assert twice(fd, 12) == 48
# CLOSURES
# ========
fun make()
let counter = 0
fun inner()
counter = counter + 1
return counter - 1
end
return inner
end
let c = make()
assert c() == 0
assert c() == 1
assert c() == 2
fun make_val(init)
let counter = init
fun inner()
counter = counter + 1
return counter - 1
end
return inner
end
let d = make_val(7)
let e = make_val(3)
assert d() == 7
assert e() == 3
assert d() == 8
assert e() == 4
assert d() == 9
assert e() == 5