variable x: code;
let x=value code
{
<print "Hi">;
};
Note that the assignment must end with a semicolon, since it is a regular assignment. Code is just one more type. You could assign variable with some code, execute it, then assign it with a different code, and again execute it. The same variable. We do not need pointers to functions!
variable x: code;
let x=value code
{
<print "Hi">;
};
<x>;
We could as well execute a code literal, directly, without assigning it to a variable. This is less commonly used in Chomik, but it is possible.
value code
{
<print "Hi">;
};
or, more explicitly:
execute value code
{
<print "Hi">;
};
Note that within a code literal you can have assignments or executions, or even declarations. No problem. You only have to remember that Chomik does not support local variables. All the variables are global.
We will now learn a pattern that is very common in Chomik.
variable do something on true:code, do something on false: code;
let do something on true=value code
{
<print "it is TRUE!">;
};
let do something on false=value code
{
<print "it is FALSE!">;
};
#
# ....
#
variable our condition:boolean;
let our condition=value boolean true;
<do something on <our condition>>;
let our condition=value boolean false;
<do something on <our condition>>;
It will print out:
it is TRUE! it is FALSE!But we call <do something on <our condition>> twice, only changing the 'our condition' variable value! Yes. You are right. There is no conditional instruction in Chomik.
We will talk now about the placeholders. Placeholders are like handy small variables that you provide with the information of their type and let Chomik to execute your code for every possible value of them. Let us use first a very simple case - the above example will be written in a more efficient way:
variable do something on (X:boolean):code;
let do something on true=value code
{
<print "it is TRUE!">;
};
let do something on false=value code
{
<print "it is FALSE!">;
};
#
# ....
#
variable our condition:boolean;
let our condition=value boolean true;
<do something on <our condition>>;
let our condition=value boolean false;
<do something on <our condition>>;
It is our example for a conditional instruction equivalent in Chomik, but the first line is different. It contains a placeholder X of type boolean. The code does exactly the same, but it does not require you to declare the two variables, one for 'false' and one for 'true'. Is it any good for us? It is. The placeholders are a powerful tool in Chomik. Take a look at the following snippet:
variable do something on (X:boolean) and (Y:boolean) and (Z:boolean):code;
let do something on (X:boolean) and (Y:boolean) and (Z:boolean)=value code
{
<print "something is FALSE!">;
};
let do something on true and true and true=value code
{
<print "all are TRUE!">;
};
#
# ....
#
variable a:boolean, b:boolean, c:boolean;
let a=value boolean true;
let b=value boolean false;
let c=value boolean true;
<do something on <a> and <b> and <c>>;
let b=value boolean true;
<do something on <a> and <b> and <c>>;
The above snippet will print out:
something is FALSE! all are TRUE!What happened? We used the placeholders X,Y and Z, all of type boolean, not only in a declaration but also in the assignment. Chomik created all possible tuples, i.e.:
false false false false false true false true false false true true true false false true false true true true false true true trueIn other words it created a cartesian product of these sets (boolean x boolean x boolean). Then the second assignment simply overwrites the code 'do something on true and true and true'.