+
1
|
list
|
skin
|
login
|
editor
α-wwwiki
::
define
user:none
(3744 bytes)
_h1 define {span {@ style="font-size:0.6em;"}(see [[lambda]] and [[defun]])} _p The alphawiki's language, "Lambdatalk", recognizes the majority of the HTML tags, a few MathML tags and some more specific to a wiki context. Four special operators {b « define, lambda, defun, alpha »} are given to extend the lambdatalk's operators set making it possible to code inline functions which can be called in the respect of the °°{first rest}°° syntax. The following examples illustrate how "define" can be used. There are a few conditions to satisfy : {pre • the syntax to follow is : °°{define name (args) •• body ••}°° where : • name is the name of the function • args is a list of arguments separated by spaces, • body is any sequence of JS expressions.. } _p {b Note 1)} The new lambdatalk's "define" function differs from the previous lambdatalk's one in the fact that the body doesn't contain lambdatalk expressions, but JS expressions. If we are no more in the Lisp's spirit, we have in hands all the JS potentialities ... which is a Lisp in "C" clothes ! See also pages [[lambda]] and [[defun]]. _p {b Note 2)} The HTML tag "input" gives another way for writing inline JS code, see pages [[input]], and more in this wiki. _h2 some examples {pre _h4 carre °°{define carre (x) return x*x;}°° -> {define carre (x) return x*x;} °°{carre 12}°° -> {carre 12} _h4 cube °°{define cube (x) return x*x*x;}°° -> {define cube (x) return x*x*x;} °°{cube 12}°° -> {cube 12} _h4 cosinus °°{define cos (a) var res = Math.cos( Math.PI/180*a ); return (Math.abs(res)< 0.0001)? 0 : res; }°° -> {define cos (a) var res = Math.cos( Math.PI/180*a ); return (Math.abs(res)< 0.0001)? 0 : res; } °°{cos 0}°° -> {cos 0} °°{cos 45}°° -> {cos 45} °°{cos 90}°° -> {cos 90} °°{cos 135}°° -> {cos 135} °°{cos 180}°° -> {cos 180} _h4 hypothenuse c{sup 2}=a{sup 2}+b{sup 2} °°{define hypo (a b) return Math.sqrt( a*a+b*b ); }°° -> {define hypo (a b) return Math.sqrt( a*a+b*b ); } °°{hypo 3 4}°° -> {hypo 3 4} _h4 equation ax{sup 2}+bx+c=0 °°{define equation (a b c) var d = b*b - 4*a*c; var res = 'no real roots'; if (d>=0) res = 'x0=' + (-b - Math.sqrt( d ))/(2*a) + ' and x1=' + (-b + Math.sqrt( d ))/(2*a); return res; }°° -> {define equation (a b c) var d = b*b - 4*a*c; var res = 'no real roots'; if (d>=0) res = 'x0=' + (-b - Math.sqrt( d ))/(2*a) + ' and x1=' + (-b + Math.sqrt( d ))/(2*a); return res; } °°{equation 1 -1 -1}°° -> {equation 1 -1 -1} °°{equation 1 -1 1}°° -> {equation 1 -1 1} _h4 factorielle n! °°{define fac (a) a = parseInt( a ); var _fac = function (n) { return (n< 1)? 1 : n*_fac(n-1); } return 'fac(' + a + ') = ' + _fac(a); }°° -> {define fac (a) a = parseInt( a ); var _fac = function (n) { return (n< 1)? 1 : n*_fac(n-1); } return 'fac(' + a + ') = ' + _fac(a); } °°{fac 6}°° -> {fac 6} } _h4 notes _ul {b Note 1)} As it is shown in pages [[parser]] and [[substitution]], the engine catches the s-expressions °°{first rest}°° from the innermost leaves to the root. As it can be seen in the previous example, the _fac() a Javascript code may contain braces which could be in conflict with the parser's engine. To avoid this issue, braces are automatically "desactivated" in a pre-process. As it is in the LISP language, "define" operator is a special form treated in a special way ! _ul {b Note 2)} The define functionality may be a serious security flaw which should be fixed. More to come ... _h4 list of lambdatalk's operators _p And of user functions defined in the page. {lib}