lambdaspeech
::
primitives
1
|
list
|
login
|
load
|
|
_img data/mustangs.jpg _h1 primitives | [[polymorphism]] _p As a dialect of {b λ-calculus} the '{lambda talk} syntax is initially reduced to abstractions and applications working on sequences of words and an empty dictionary. User defined functions can be built to add data structures (pairs, lists, trees), control structures (nil?, recursion, Y-combinator,...), numbers and so on, theoretically {i ad libitm}. For obvious reasons of efficiency it's a good thing to implement them as JS primitive functions and add new ones built on the capabilities of web browsers like HTML, CSS, SVG and more. _p Currently the '{lambda talk}'s Dictionary contains {b 170} built-in primitives. Thanks to this set of primitives the user can define new functions using the '{lambda talk} syntax and its set of 9 special forms, [{code lambda, def, if, let, quote|', macro, script, style, require}]. For instance the {b factorial} function: {pre '{def factorial {lambda {:n} {if {< :n 1} then 1 else {* :n {factorial {- :n 1}}}}}} -> {def factorial {lambda {:n} {if {< :n 1} then 1 else {* :n {factorial {- :n 1}}}}}} '{factorial 6} -> {factorial 6} } _p The dictionary can also be extended {i inline} using the special form {code '{script JS code}}. For instance we could add the {code factorial} function as a primitive: {pre LAMBDATALK.DICT['factorial'] = function() '{ var fac = function(n) { return (n<1)? 1 : n*fac(n-1) }; var n = arguments[0]; return fac(n) }; } _p and write {code '{factorial 6}} to display {b 720} in the wiki page. Thanks to the {code require} special form, external code can be used to overcome limits of JS numbers. For instance, the {b lib_BN} wiki page contains a powerful set of functions working on Big Numbers written by a clever coder, {i Jonas Raoni Soares Silva}: {require lib_BN} {prewrap '{require lib_BN} '{factorial 90} -> {factorial 90} '{BN.fac 90} -> {BN.fac 90} } _p It's a matter of choice and efficiency. _h2 the dictionary _p The dictionary's content is listed using {code '{lib}}: {prewrap {lib} } _p Note that the dictionary contains {b primitives}, ie Javascript predefined functions, followed by user defined functions, ie '{lambda talk} written functions. The former end at {code drag} and, in the current page, the latter begin at {code factorial}' which is the first function defined in this page. _h1 Categories _p The primitives are listed below _ul 1. WORD: words and sequences of words _ul 2. ARRAY: built on the JS array _ul 3. PAIR & LIST: another Lisp-like type built on JS arrays _ul 4. MATH: built on the JS Math object _ul 5. HTML: built on standard HTML tags _ul 6. GRAPHICS: built on canvas and svg _ul 7. MISC: some others _h2 1. WORD {pre '{equal? abc abc} -> true '{equal? abc xyz} -> false '{empty? abc} -> false '{empty? } -> true '{chars abc} -> 3 '{charAt 0 abc} -> a '{charAt 2 abc} -> c '{charAt 3 abc} -> '' '{substring 1 3 abcdef} -> bcd '{length ab cd ef gh} -> 4 '{first ab cd ef gh} -> ab '{rest ab cd ef gh} -> cd ef gh '{last ab cd ef gh} -> gh '{nth 0 ab cd ef gh} -> ab '{nth 3 ab cd ef gh} -> gh '{nth 4 ab cd ef gh} -> undefined '{replace ab by xy in ab cd ef gh} -> xy cd ef gh } _h2 2. ARRAY {pre '{#.new a b c d e f} -> _ARRA_n displayed as [a,b,c,d,e,f] '{#.disp {#.new a b c d e f}} -> [a,b,c,d,e,f] '{#.join {#.new a b c d e f}} -> abcdef '{#.split abcdef} -> _ARRA_n displayed as [a,b,c,d,e,f] '{#.array? {#.new a b c d e f}} -> true '{#.array? a} -> false '{#.null? {#.new a b c d e f}} -> false '{#.null? {#.new}} -> true '{#.null? a} -> error ; must be fixed '{#.empty? {#.new a b c d e f}} -> false '{#.empty? {#.new }} -> true '{#.in? {#.new a b c d e f} c} -> true '{#.in? {#.new a b c d e f} g} -> false '{#.length {#.new a b c d e f}} -> 6 '{#.get {#.new a b c d e f} 0} -> a '{#.get {#.new a b c d e f} 5} -> f '{#.get {#.new a b c d e f} 6} -> undefined '{#.first {#.new a b c d e f}} -> a '{#.last {#.new a b c d e f}} -> f '{#.rest {#.new a b c d e f}} -> _ARRA_n displayed as [b,c,d,e,f] '{#.slice {#.new a b c d e f} 1 5} -> _ARRA_n displayed as [b,c,d,e] '{#.concat {#.new a b c} {#.new d e f}} -> _ARRA_n displayed as [a,b,c,d,e,f] '{#.set! {#.new a b c d e f} 1 x} -> _ARRA_n displayed as [a,x,c,d,e,f] '{#.addlast! {#.new a b c d e f} x} -> _ARRA_n displayed as [a,b,c,d,e,f,x] alias: #.push! '{#.sublast! {#.new a b c d e f}} -> _ARRA_n displayed as [a,b,c,d,e] alias: #.pop! '{#.addfirst! {#.new a b c d e f} x} -> _ARRA_n displayed as [x,a,b,c,d,e,f] alias: #.unshift! '{#.subfirst! {#.new a b c d e f}} -> _ARRA_n displayed as [b,c,d,e,f] alias: #.shift!, '{#.reverse! {#.new a b c d e f}} -> _ARRA_n displayed as [f,e,d,c,b,a] '{#.sort! > {#.new 9 2 1 3 5 8} } -> _ARRA_n displayed as [1,2,3,5,8,9] '{#.sort! < {#.new 9 2 1 3 5 8} } -> _ARRA_n displayed as [9,8,5,3,2,1] } _h2 3. PAIR & LIST {pre '{pair a b} -> _PAIR_n displayed as (a b) // alias: cons '{pair? a} -> false '{pair? {pair a b}} -> true '{nil? nil} -> true '{nil? {pair a b}} -> false '{left {pair a b}} -> a // alias: car '{right {pair a b}} -> b // alias: cdr '{pair.disp {pair a b}} -> (a b) '{pair.disp a} -> a '{list.new a b c d e f} -> _PAIR_n displayed as (a (b (c (d (e (f nil)))))) '{list.disp {list.new a b c d e f}} -> a b c d e f '{list.null? nil} -> true '{list.null? {list.new a b c d e f}} -> false '{list.length {list.new a b c d e f}} -> 6 '{list.first {list.new a b c d e f}} -> a '{list.rest {list.new a b c d e f}} -> _PAIR_n displayed as (b (c (d (e (f nil))))) // alias: butfirst '{list.last {list.new a b c d e f}} -> f '{list.butlast {list.new a b c d e f}} -> not yet implemnted '{list.reverse {list.new a b c d e f}} -> _PAIR_n displayed as (f (e (d (c (b (a nil)))))) } _p See [[lib_list]] for more user defined functions on lists. _h2 4. MATH {pre '{+ 1 2 3} -> {+ 1 2 3} '{* 1 2 3} -> {* 1 2 3} '{- 1 2 3} -> {- 1 2 3} '{/ 1 2 3} -> {/ 1 2 3} '{% 4 1} -> {% 4 1} '{% 4 2} -> {% 4 2} '{% 4 3} -> {% 4 3} '{% 4 4} -> {% 4 4} '{< 1 2} -> {< 1 2} '{> 1 2} -> {> 1 2} '{<= 1 1} -> {<= 1 1} '{>= 1 1} -> {>= 1 1} '{= 1 2} -> {= 1 2} '{= 1 1.00000} -> {= 1 1.00000} '{not true} -> {not true} '{or false false true false} -> {or false false true false}, '{and false false true false} -> {and false false true false}, '{PI} -> {PI} '{E} -> {E} '{exp 1} -> {exp 1} '{log 1} -> {log 1} '{pow 2 8} -> {pow 2 8} '{sqrt 2} -> {sqrt 2} '{cos {PI}} -> {cos {PI}} '{sin {/ {PI} 2}} -> {sin {/ {PI} 2}} '{tan {/ {PI} 4}} -> {tan {/ {PI} 4}} '{acos 0} -> {acos 0} // '{/ {PI} 2} '{asin 0} -> {asin 0} '{atan 1} -> {atan 1} // '{/ {PI} 4} '{abs -1} -> {abs -1} '{round 3.1416} -> {round 3.1416} '{ceil 3.1416} -> {ceil 3.1416} '{floor 3.1416} -> {floor 3.1416} '{min 12 3 -1 10} -> {min 12 3 -1 10} '{max 12 3 -1 10} -> {max 12 3 -1 10} '{date} -> {date} '{random} -> {random} '{serie 1 20 2} -> {serie 1 20 2} '{map sqrt {serie 1 4}} -> {map sqrt {serie 1 4}} '{reduce {lambda {x y} {+ x y}} {serie 1 10}} -> {reduce {lambda {x y} {+ x y}} {serie 1 10}} } _h2 5. HTML _p A subset of HTML tags are translated into '{lambda talk} syntax. New HTML tags can be added alike following the process described in introduction. _h3 b, i, u, del, code, sup, sub {{BLOCK}{div {{CODE} I just say '{b Hello} World} {{VIEW} I just say {b Hello} World}}} {{BLOCK}{div {{CODE} I just say '{i Hello} World} {{VIEW} I just say {i Hello} World}}} {{BLOCK}{div {{CODE} I just say '{u Hello} World} {{VIEW} I just say {u Hello} World}}} {{BLOCK}{div {{CODE} I just say '{del Hello} World} {{VIEW} I just say {del Hello} World}}} {{BLOCK}{div {{CODE} I just say '{code Hello} World} {{VIEW} I just say {code Hello} World}}} {{BLOCK}{div {{CODE} I just say '{sup Hello} World} {{VIEW} I just say {sup Hello} World}}} {{BLOCK}{div {{CODE} I just say '{sub Hello} World} {{VIEW} I just say {sub Hello} World}}} _h4 h1, h2, h3, h4, h5, h6 {{BLOCK} {{CODE} '{h1 Hello World} '{h2 Hello World} '{h3 Hello World} '{h4 Hello World} '{h5 Hello World} '{h6 Hello World} } {{VIEW} {h1 Hello World} {h2 Hello World} {h3 Hello World} {h4 Hello World} {h5 Hello World} {h6 Hello World} }} _p {b Alternative}: {code _{span}h1 Hello World } followed by a carriage return, ↵. _h4 p, br, hr, center, pre, prewrap, blockquote, textarea, hide {{BLOCK}{{CODE} '{p Paragraphs are sequences of words between two carriage returns. They are justified and the first line starts with an alinea. An alternative can be used avoiding curly braces. Just begin with _{span}p and end with a carriage return.} '{p Paragraphs are sequences of words between two carriage returns. They are justified and the first line starts with an alinea.} } {{VIEW} {p Paragraphs are sequences of words between two carriage returns. They are justified and the first line starts with an alinea. An alternative can be used avoiding curly braces. Just begin with _{span}p and end with a carriage return.} {p Paragraphs are sequences of words between two carriage returns. They are justified and the first line starts with an alinea.} }} _p {b Alternative}: {code _{span}p some text... } followed by a carriage return, ↵. {{BLOCK}{{CODE} Hello'{br}World} {{VIEW} Hello{br}World}} {{BLOCK}{{CODE} Hello'{hr}World} {{VIEW} Hello{hr}World}} {{BLOCK}{{CODE} '{center Hello World}} {{VIEW} {center Hello World}}} {{BLOCK}{{CODE} '{pre I just say Hello ... World }} {{VIEW} {pre I just say Hello ... World }}} {{BLOCK} {{CODE} '{prewrap The
prewrap
function is alike the
pre
function but wraps long texts inside the wiki page, even for long strings without spaces.}} {{VIEW} {prewrap The {b prewrap} function is alike the {b pre} function but wraps long texts inside the wiki page, even for long strings without spaces.}}} {{BLOCK}{{CODE} '{blockquote Hello World}} {{VIEW} {blockquote Hello World}}} {{BLOCK}{{CODE} '{textarea Hello World}} {{VIEW} {textarea Hello World}}} {{BLOCK}{{CODE} '{{hide} Hello World}} {{VIEW} {{hide} Hello World} // the content is hidden}} _h4 span, div, @ {{BLOCK}{{CODE} I just say '{span {@ style="font:bold 2.0em georgia; color:red;"}Hello} World!} {{VIEW} I just say {span {@ style="font:bold 2.0em georgia; color:red;"}Hello} World!}} {{BLOCK}{{CODE} I just say '{div {@ style="font:bold 2.0em georgia; color:red;"}Hello} World!} {{VIEW} I just say {div {@ style="font:bold 2.0em georgia; color:red;"}Hello} World!}} _p {b Note:} The {b @} function (@ for {b at}tribute) can be used in every other HTML functions. _h4 a, img, input {{BLOCK}{{CODE} '{a {@ href="http://www.pixar.com"}PIXAR}} {{VIEW} {a {@ href="http://www.pixar.com"}PIXAR}}} _p {b Alternative}: {code [{span}[PIXAR|http://www.pixar.com]]} {{BLOCK}{{CODE} '{a {@ href="?view=start"}START}} {{VIEW} {a {@ href="?view=start"}START}}} _p {b Alternative}: {code [{span}[start]]} used to create and access a wiki page {{BLOCK}{{CODE} '{img {@ src="data/mustangs.jpg" width="100%"}}} {{VIEW} {img {@ src="data/mustangs.jpg" width="100%"}}}} _p {b Alternative}: {code _{span}img data/mustangs.jpg↵} {{BLOCK}{{CODE} '{input {@ type="button" value="HI" onclick="alert('Hello World')"}}} {{VIEW} {input {@ type="button" value="HI" onclick="alert('Hello World')"}}}} _p More can be seen elsewhere in the wiki. _h4 ul, ol, dl, table {{BLOCK} {div {{CODE} '{ul {li item 1} {li item 2 {ul {li item 2.1} {li item 2.2} } } {li item 3} } } {{VIEW} {ul {li item 1} {li item 2 {ul {li item 2.1} {li item 2.2} } } {li item 3} } }} } _p {b Alternative}: {{BLOCK}{{CODE} _{span}ul item 1↵ _{span}ul item 2↵ _{span}ul20 item 2.1↵ _{span}ul20 item 2.2↵ _{span}ul item 3↵ }{{VIEW} _ul item 1 _ul item 2 _ul40 item 2.1 _ul40 item 2.2 _ul item 3 }} {{BLOCK} {div {{CODE} '{ol {li item 1} {li item 2 {ol {li item 2.1} {li item 2.2} } } {li item 3} } } {{VIEW} {ol {li item 1} {li item 2 {ol {li item 2.1} {li item 2.2} } } {li item 3} } }} } {{BLOCK} {{CODE} '{dl {dt Coffee} {dd Black hot drink} {dt Milk} {dd White cold drink} } } {{VIEW} {dl {dt Coffee} {dd Black hot drink} {dt Milk} {dd White cold drink} } }} {{BLOCK} {div {{CODE} '{table {tr {td 11} {td 12} {td 13}} {tr {td 21} {td 22} {td 23}} {tr {td 31} {td 32} {td 33}} } } {{VIEW} {table {tr {td 11} {td 12} {td 13}} {tr {td 21} {td 22} {td 23}} {tr {td 31} {td 32} {td 33}} } }} } {{BLOCK}{{CODE} {quote {select {@ id="select"} {option {@ value="volvo"}Volvo} {option {@ value="saab"}Saab} {option {@ value="opel"}Opel} {option {@ value="audi"}Audi} } {input {@ type="button" value="selected option" onclick=" document.getElementById('output').innerHTML = 'Current selection is ' + document.getElementById('select').value + ' car.'"}} {div {@ id="output"}} } }{{VIEW} {pre {select {@ id="select"} {option {@ value="volvo"}Volvo} {option {@ value="saab"}Saab} {option {@ value="opel"}Opel} {option {@ value="audi"}Audi} } {input {@ type="button" value="selected option" onclick=" document.getElementById('output').innerHTML = 'Current selection is ' + document.getElementById('select').value + ' car.'"}} {div {@ id="output"}} } }} _h2 6. GRAPHICS {prewrap canvas, svg, line, rect, circle, ellipse, polygon, polyline, path, text, g, mpath, use, textPath, pattern, image, clipPath, defs, animate, set, animateMotion, animateTransform, title, desc, } _p See [[canvas]], [[svg]] and some other pages using SVG functions. _h2 7. MISC {pre lib: used in this page to display the current primitives and user defined functions require: see [[require]] audio, video, source: see [[sounds]] iframe: see [[iframe]] turtle: see [[turtle]] & followings long_mult: see [[numbers]] & followings drag: see [[drag]] } _p {b Note:} Numerous examples are shown in the wiki. Have a look! {{hide} {def BLOCK div {@ style="box-shadow:0 0 8px #000"}} {def CODE pre {@ style="padding:5px; font:normal 1.0em courier; background:#eee; display:pre-wrap;"}} {def VIEW div {@ style="padding:5px; font:normal 1.0em optima;"}} }
lambdaspeech v.20200126