skopy/features/fun.sk

88 lines
1019 B
Plaintext
Raw Normal View History

var a = fun (x)
return x * 2
end
assert a(7) eq 14
assert a(a(7)) eq 28
var b = fun (u, v)
return u - v
end
assert b(a(2), a(3)) eq -2
var c = fun(x, f)
return f(f(x))
end
assert c(7, a) eq 28
var d = fun(x)
var a = fun(x)
x + 1
end
return a(a(x))
end
assert d(3) eq 5
var e = fun(n)
if n == 0
1
else
n * e(n - 1)
end
end
assert e(5) eq 120
var f = fun(x, n)
x(x(n))
end
assert f(fun(x) x * 2 end, 2) eq 8
2024-04-05 16:31:11 +00:00
var g = fun (init)
var count = init
var inner = fun ()
count = count + 1
count - 1
end
return inner
end
var h = g(5)
var i = g(0)
assert h() eq 5
assert i() eq 0
assert h() eq 6
assert i() eq 1
assert h() eq 7
assert i() eq 2
assert h() eq 8
assert i() eq 3
var j = fun (count)
return fun ()
count = count + 1
count - 1
end
end
var k = j(37)
assert k() eq 37
assert k() eq 38
assert k() eq 39
assert k() eq 40
assert k() eq 41
fun l(x)
3 * x
end
assert l(7) eq 21