/**************************************************************************** Kalkulon - A programmable calculator for programmers This is an example Kalkulon script. To load from within Kalkulon: Load("examples/scriptname.k") then you can call functions like: funcname() or funcname(arg0, ...) Author: Juergen Holetzeck 2003 - 2007 e-mail: contact@kalkulon.de homepage: www.kalkulon.de ****************************************************************************/ // some simple function definitions add(x,y) = x+y; add(x,y,z) = x+y+z; // function with use of local variable // BEWARE: expressions inside of a function definition are seperated by comma (,) // and not semicolon(;)! sin_deg(deg)=(PI=2*acos(0), sin(deg/180*PI)); // Fibonacci // f0=0, f1=1, fn= fn-1 + fn-2 fib(n) = ( if( n<=1; res = n; res=fib(n-1)+fib(n-2) ), res ); /* Some ways to write a factorial function (for small values of n only!) */ // iterative (n>=1) fak1(n) = (result=1, while(n; result=result*n--)); fak2(n) = (result=1, do(result=result*n; --n)); fak3(n) = for(result=1, i=1; i<=n; i++; result=result*i); // recursive (n>=0) fak4(n) = if( n<=1; 1; n*fak4(n-1)); // functional (n>=1) fak5(n) = Fold(Table(n, '#'), 1, '#1*#2');