I think the most important thing about the previous code is creating mutable reference that can be passed around. This is fine in most programming languages however it does not seem to be a design choice of Lil at all to have this design pattern.
By putting global variable inside of functions we can create a reference.
Then it is also possible to modify and get the value of the reference.
# Create a reference
on create_ref do
# we give ourselves the possibility to apply a function on the reference
on _ f do
x : f[x]
end
end
# sets the value of a reference "ref" to "val"
on set ref val do
ref[on _ x do val end]
end
# get the value of the reference "ref"
on get ref do
ref[on id x do x end]
end
# this reference can be passed around in function argument
a : create_ref[]
set[a 0] # 0
set[a 1] # 1
# here we copy the reference
b : a
# when b is modified, a is also modified
set[b 42] # 42
get[a] # 42
Then we can pass this reference around in functions :
on complex_computation ref do value_res : ... # result of the computation # instead of returning the computation we can modify # the reference to get the result just like a buffer # given in argument in a C program for example set[ref value_res] nil end buffer : create_ref[] complex_computation[buffer] # returns nil get[buffer] # gets us the value of the computation