λ-wiki
::
math
+
| 1 |
help
|
pages
|
skin
|
login
|
code
www!k! v.20120610
! CE SITE NECESSITE JAVASCRIPT !
λ-wiki :: éditeur
{center [[maths]] [[functions]] [[booleans]] [[iteration]] [[recursion]] } _h1 functions and vars _p It is now possible to play in a wiki page using a syntax close to LISP's one with math expressions containing numbers, functions and variables. There are two classes of functions and variables : static and dynamic. _ul {b static} functions and variables are hard-coded in the parser of lambdawiki. This is the list : [{lib core}]. _ul {b dynamic} functions and variables can be {b defined} and {b called} in a wiki page. Their list is visible at the end of this page. _p {b Note} : functions and variables are both {b data} stored in the same way (actually in two hash arrays : core_func[] and dyna_func[]). There is nothing different between them except the fact that functions have arguments, for instance °°{add 1 2}°° - > 3; variables are functions without arguments, for instance °°{PI}°° - > 3.14... . Static variables are actually {b constants}, immutables de facto. Dynamic variables can be changed by a simple re-definition in the page. _h6 1) writing with static functions and vars _p Math expressions can be written inline : {table {tr {@ font:bold 1em arial; text-align:center;} {td {@ width:75%;} writing this expression} {td {@ width:36%;} produces this result} } {tr {td Sum of a sequence of integers : °°{add 1 -2 -3 -4 5}°° - >} {td {add 1 -2 -3 -4 5}} } {tr {td Product of a sequence of integers : °°{mult 1 2 3 4 5}°° - >} {td {mult 1 2 3 4 5}} } {tr {td Quotient of two integers : °°{quot 1 2}°° et °°{quot 1 0}°° - >} {td {quot 1 2} et {quot 1 0}} } {tr {td Square root of an integer : °°{sqrt 2}°° - >} {td {sqrt 2}} } {tr {td Hypotenuse of a right triangle: °°{sqrt {add {mult 3 3} {mult 4 4}}}°°} {td {sqrt {add {mult 3 3} {mult 4 4}}}} } {tr {td Factorial N! : °°{fact 10}°° - >} {td {fact 10}} } {tr {td The number π : °°{PI}°° - >} {td {PI}} } } _h6 2) working with dynamic functions and vars _p It's also possible to define functions and call them afterwards. {table {tr {@ font:bold 1em arial; text-align:center;} {td {@ width:75%;} writing this expression} {td {@ width:36%;} produces this result} } {tr {td Defining a variable : °°{define N = 1000}°°} {td {define N = 1000} } } {tr {td Getting a variable : °°{N}°°} {td {N} } } {tr {td Sum of integers to N : °°{quot {mult {N} {add {N} 1}} 2}°°} {td {quot {mult {N} {add {N} 1}} 2} } } {tr {td A function add built with primitives incr and decr: {br} 1) defining : {define myadd x y = (if (equal x 0) then y else (add (decr x) (incr y)))} {br} 2) calling : '{myadd 99 1}' - > } {td {myadd 99 1}} } {tr {td A function returning the sum of the integers from 1 to N : {br} 1) defining : {define sum n = (quot (mult n (add n 1)) 2) } {br} 2) calling : '{sum {N}}' - > } {td {sum {N}}} } {tr {td A function returning the diagonal of a rectangle (x y) : {br} 1) defining : {define diag2 x y = (sqrt (add (mult x x) (mult y y))) } {br} 2) calling : '{diag2 3 4}' - > } {td {diag2 3 4}} } {tr {td A function returning the diagonal of a cube (x y z) : {br} 1) defining : {define diag3 x y z = (sqrt (add (mult x x) (mult y y) (mult z z))) } {br} 2) calling : '{diag3 1 1 1}' - > } {td {diag3 1 1 1}} } } {hr} _h6 listing of vars and functions defined in this page _ul {lib dyn}