Friday, October 2, 2015

cat used in named functions

"cat" can also be named as previously said.  These examples show its uses.  note the examples are for demonstration purposes; certainly anything can be named.

To define/name a function, use the ":"
for example, I would like to name a function "testJZ", which would take 5 arguments.  It would be like (note the following function is not doing any meaningful thing except returning a value of 1)

testJZ(5): cat(ifthenelse(#1*#2*#3*#4*#5.eq.0, 1, testJZ(#1+1,#2+2,#3+3,#4+4,#5+5)))

where the arguments are referred to as # and the order they appear in the definition statement. 
The logic check .eq. means equals to; or eq(exp1, exp2), returning 1 if they are equal; or 0 if they are not.

another example, to test how powerful your computer, is.

testJZ(1):cat(#1+1+testJZ(#1+1))

certainly the problem arises if you define testJZ(10000000)


_____________________________
The following paragraphs in italics are from bgmn.de:

name(argumentcount):expression
A positive count of argument defines a fixed value, the absolute value of a negative argument count defines a lower limit of argument count. expression may contain the special symbols
#1 #2...
which are replaced by the evaluated numeric value of the first, second... argument.

Every argument is evaluated only once. An erroneous argument causes an error only if it will be used. Example:

faculty(1):ifthenelse(eq(#1,0),1,#1*faculty(#1-1))
or using level 3.3.29 or above:

faculty(1):cat(n==#1,ret==1,while(n,ret==ret*n,n==n-1),ret)
 
Note, the word faculty in the above functions is normally called factorial on this side of the ocean.  Normally it is realized via recursive method as in the example. 
 
Factorial(N)=N*(N-1)*(N-2)*..............*3*2*1

No comments:

Post a Comment