show example
' a lua-like functional programming language
' factorial function
fn fact(n) ::
if n = 0 then
1
else
n * fact(n-1)
' read a number from the console
and let a :: read() in
a . toNum . fact . print
' an iterative factorial function
and fn fact2(n) ::
range(1, n+1)
. foldl(1, mul)
' read a number from the console
and let a :: read() in
' composition of functions
a . toNum . fact2 . print
' obligatory fibonacci function
and fn fib(n) ::
if n < 2 then
n
else
fib(n-1) + fib(n-2)
and let a :: read() in
a . toNum . fib . print
' test of currying
and fn test(a, b, c) ::
print(a + b + c)
and let _ :: () in
test(1)(2)(3) ' 6
' test of assignment
and let c :: 0 in
c :: 3 in
print(c) ' 3
' test of closures
and fn counter(i) ::
fn () ::
i :: i + 1 in i
end
' end keyword prevents the parser from
' attaching the next 'and' to the inner function,
' could also just use parentheses
' test of closures
and let c :: counter(0) in
print(c()) ; ' 1
print(c()) ; ' 2
print(c()) ' 3
' test of lists
and let l :: [1, 2, 3] in
print(l) ;
' [1, 2, 3]
print(l . head) ;
' 1
print(l . tail) ;
' [2, 3]
print(l .. [4, 5, 6]) ;
' [1, 2, 3, 4, 5, 6]
print(l.nth(1)) ' 2
' and a whole other bunch of list functions
' test of regex
and let expr :: regex("a*b") in
print(expr.match("aaab")) ; ' true
print(expr.match("b")) ; ' true
print(expr.match("ab")) ; ' true
print(expr.match("c")) ; ' false
zy is a functional programming language with a syntax inspired by lua and ocaml.
it is currently in development and has a (slow) reference interpreter written in kotlin.
the source code is available on github.
all contributions are welcome.
zy is:
zy has:
the standard library is currently somewhat small, but it does have a few useful functions.
print(arg)
- prints arg
to stdout.read()
- reads a line from stdin.add(a, b)
- adds a
and b
. is the default add operator.sub(a, b)
- subtracts b
from a
. is the default subtract operator.mul(a, b)
- multiplies a
and b
. is the default multiply operator.div(a, b)
- divides a
by b
. is the default divide operator.mod(a, b)
- returns the remainder of a
divided by b
. this is the default modulo operator.eq(a, b)
- returns true
if a
is equal to b
. this is the default equality operator.neq(a, b)
- returns true
if a
is not equal to b
. this is the default inequality operator.lt(a, b)
- returns true
if a
is less than b
. this is the default less than operator.gt(a, b)
- returns true
if a
is greater than b
. this is the default greater than operator.lte(a, b)
- returns true
if a
is less than or equal to b
. this is the default less than or equal to operator.gte(a, b)
- returns true
if a
is greater than or equal to b
. this is the default greater than or equal to operator.and(a, b)
- returns true
if a
and b
are both true
. this is the default and operator.or(a, b)
- returns true
if a
or b
are true
. this is the default or operator.not(a)
- returns true
if a
is false
. this is the default not operator.compose(a, b)
- returns a function that is the composition of a
and b
. this is the default compose operator.toNum(a)
- converts a
to a number.toStr(a)
- converts a
to a string.toBool(a)
- converts a
to a boolean.global(s)
- returns the global variable s
.range(start, end)
- returns a list of numbers from start
to end
.nth(n, l)
- returns the n
th element of l
.head(l)
- returns the first element of l
.tail(l)
- returns the last element of l
.len(l)
- returns the length of l
.map(l, f)
- returns a list of the result of applying f
to each element of l
.foldl(a, f, l)
- returns the result of applying f
to each element of l
from left to right, starting with a
.foldr(a, f, l)
- returns the result of applying f
to each element of l
from right to left, starting with a
.filter(l, f)
- returns a list of the elements of l
for which f
returns true
.reduce(f, l)
- returns the result of applying f
to each element of l
from left to right.zip(l1, f)
- returns a list of pairs of elements from l1
and l2
.zipWith(l1, l2, f)
- returns a list of the result of applying f
to each pair of elements from l1
and l2
.plus(l1, a)
- returns the list l1
with a
appended to the end.minus(l1, a)
- returns the list l1
with the first instance of a
removed.drop(l1, n)
- returns the list l1
with each instance of a
removed.take(l1, n)
- returns the list l1
with the first n
elements removed.slice(l1, n, m)
- returns the list l1
with the first n
elements removed and the first m
elements removed.concat(a, b)
- returns the concatenation of a
and b
. supports strings too. this is the default concatenation operator.reverse(l)
- returns the reverse of l
.sort(l)
- returns the sorted version of l
.regex(a)
- returns a regex object from a
.match(r, s)
- returns true
if r
matches s
.replace(r, s, t)
- returns s
with anything that r
matches replaced with t
.split(r, s)
- returns a list of strings from s
split by r
.join(s, l)
- returns a string from l
joined by s
.