f
defined byf(n):=sum(1..n,i,i^2)
n
squares. For instance, after this definition, f(4)
evaluates to 30
.a
and b
are two-dimensional vectors and draws a square whose edge is defined by these two vectors:sq(a,b):=( n=(b-a); n2=(-n_2,n_1); draw(a,b); draw(a,a-n2); draw(b,b-n2); draw(a-n2,b-n2); )
(statement_1;…;statement_k)
. Furthermore, the function uses the variables n
and n2
. These variables are created when the function is first called. However, they are (by default) not local. Their values are visible also after the function has been called.mean(a,b,c):=( sum=a+b+c; sum/3; )
mean([3,4],[2,7],[4,7])
evaluates to [3,6]
.fac(n):=if(n==0,1,n*fac(n-1));
gcd(a,b):=if(b==0, //End of recursion reached a, //Then return the number a if(b>a, //Perhaps switch parameters gcd(b,a), //switched version gcd(b,mod(a,b)) //Recursion ) );
x=3; b=[x^2,x^3]; c=2*b;
x
the value 3
, to b
the value [9,27]
, and to c
the value [18,54]
. A variable defined in a function remains visible also outside the scope of the function. Exceptions to this rule are the parameters of the function and variables explicitly defined local. The following program exemplifies the scope of variables:f(x):= ( x=x+x; println(x); y="User" ); x="Hello "; y="World"; println(x+y); f(x); println(x+y);
Hello World Hello Hello Hello User
regional(…)
operator.y
is defined to be a local variable within the function:f(x):= ( regional(y); x=x+x; println(x); y="User"; ); x="Hello "; y="World"; println(x+y); f(x); println(x+y);
Hello World Hello Hello Hello World
a=3; timesa(x):= x*a; println(timesa(2)); a=5; println(timesa(2));
6 10
timesa(2)
depends on the actual value of the (global) variable a
at the moment the function is evaluated. So after redefining a
the behavior of the function timesa
changes. Sometimes this effect is intended, sometimes it is not. It may happen that one wants to freeze the behavior of a function to depend on the values of the variables at the moment when the function was defined. This can be achieved by using the operator ::=
to define the function. This operator copies the entire variable assignments and binds them to the function. Therefore, the programa=3; timesa(x)::= x*a; println(timesa(2)); a=5; println(timesa(2));
6 6
a
is restored. This binding process does not only extend to all variables used in the function itself. It extends to all variables that may be relevant to the execution of the function.a
can be set explicitly using a modifier. An example thereof can be seen in the following piece of code:a=3; timesa(x)::= x*a; println(timesa(2)); println(timesa(2,a->10));
6 20
pi
or the imaginary unit i
. These constants are predefined as variables in CindyScript. This allows to write a complex number for instance as 3+i*5
. However, different values can be assigned to those variables. For example, it is still possible to use these variables as run variables in loops. The following program illustrates this feature:println(i); repeat(4,i,println(i)); println(i);
0 + i*1 1 2 3 4 0 + i*1
i
is overwritten, then it is still possible to access the complex unit using the function complex([0,1])
. Other predefined variables are true
and false
for the logical constants, as well as the empty list, nil
.A
. More detailed information on this topic may be found in the section on Accessing Geometric Elements.:
operator. This is a simple but very powerful feature. After the colon an arbitrary string value can be added as a key to access the data. This key serves as a variable to which arbitrary values may be attached.A:"age"=17; B:"age"=34; A:"haircolor"="brown"; B:"haircolor"="blonde";
forall(allpoints(),p, println(p:"age"); println(p:"haircolor"); )
17 brown 34 blonde
keys(...)
operator. So in the above example the codeprint(keys(A));
["age","haircolor"];
a=[]; a:"data"=18 print(a:"data")
Page last modified on Monday 09 of February, 2015 [13:59:50 UTC].
The original document is available at
http://doc.cinderella.de/tiki-index.php?page=Variables%20and%20Functions