Note:
This introduction assumes that you have previous knowledge about programming and programmatic functionalities. For further information and guidance refer to Till Programming Reference.
Till programming is for everyone! From total noobs who just want to play with a programming language and learn the basics, to most experienced programmers who want to put their knowledge and experience into action with a fascinating language that breaks the boundaries and limitations of other languages.
Let’s go through a mini project that introduces the language in a step-by-step basis. The goal is to calculate π with a famous summation of alternating signed reciprocals of odd integers:
! PI CALCULATOR
! variables
calculated_pi
sum 0
! calculate 4 * (1 - 1/3 + 1/5 - 1/7)
sum ++ 1
sum -- 1/3
sum ++ 1/5
sum -- 1/7
calculated_pi 4 * sum
! print result
print calculated_pi
Usually, repetitive actions are put in a looping mechanism. Variables declaration and assignment can be aggregated:
calculated_pi sum 0
signum denominator 1
! loop 1000 times and sum reciprocals
index 1000 ~
reciprocal signum / denominator
sum ++ reciprocal
signum ** -1
denominator ++ 2
^
calculated_pi 4 * sum
print 'the result is: '+ calculated_pi
! output: the result is: 3.14059265383979
Since higher number of iterations yields more accurate result, it will be efficient to put all calculation activity in a function:
calculated_pi
go: print 'calculated π: ' + calculated_pi calc 1000
calc iter_num:
sum 0
signum denominator 1
index iter_num ~
sum ++ signum / denominator
signum ** -1
denominator ++ 2
^
4 * sum
Because we use functions in our code, it is appropriate to create a starting function like the go function, that will be the active function that will run on program start (after running the opening statements section).
Also go is a one-line-function, that includes its one-and-only statement in the same line.
The calc function has one parameter iter_num. The returned value is the last statement of the function.
If it is needed to calculate how much time the program ran, we can use the now system function, which returns a record of current date and time, and has a totalsecond property. Each time, the time record is assigned by now and has the actual total seconds:
time { totalsecond 0 }
time now
print sec_start time.totalsecond
! long operation
num 0
index 1000000 ~ num ++
time now
print sec_end time.totalsecond
print sec_end - sec_start
! output:
! 153081.8314939
! 153082.1381296
! 0.306635699991602
Till has a storage module for storing data. Here is a program that stores a list, and on each run it adds a number to the stored list (“\f” denotes the boolean false):
! uncomment to remove storage nums
! comment after first run
storage.unset 'nums'
nums [ ]
storage.set 'nums' nums \f
nums storage.get 'nums'
nums_len len nums
nums <- nums_len ++
storage.set 'nums' nums
print nums
Or get from a database, by using mssqldb module:
nums []
rows []
mssqldb.connect 'Server=.SQLExpress;Database=MyDBName;Uid=MyUserName;Pwd=MyPassword;'
rlen len rows mssqldb.all 'select num from nums_table'
i rlen ~
row {}
row rows i
nums <- row 'num'
^
print nums
Till has a math module that has all mathematical functions, such as math.power, and also a math.pi that is equal to π. So it is possible to make successive calculations with growing number of iterations and check the delta:
calculated_pi
pi math.pi
go:
delta
iter_num 1000
print 'iterations: ' + iter_num
print 'calculated π: ' + calculated_pi calc iter_num
delta pi - calculated_pi
print 'delta: ' + delta
calc iter_num: ...
It is possible to log program run output with system function “@“. Replacing every print keyword with “@“, will create a file with the extension “.tilldebug”:
iterations: 1000
calculated π: 3.14059265383979
delta: 0.000999999749998981
A full working program that has all the aforementioned capabilities can be found at Pi Calculator Project.