lambdaway
::
iife
4
|
list
|
login
|
load
|
|
;; {uncover data/Hippocampus.gif 100 500 But who is this little man fighting in the center of my brain?} _h1 Text rewriting using IIFEs _p Everything in the world of coding begins, in essence, with {b text rewriting}. Here is a quick explanation of this rather peremptory statement, using a strange tool, called {b IIFE}. _h2 introduction _p Consider the following words replacement command, asked in plain english {pre replace : {b foo} and {b bar} in : My first name is {b foo} and my last name is {b bar}. by : James and Bond } _p To be understood by a computer, we must rewrite it in a shorthand notation, a codified syntax, here for instance as a [[lambdatalk|?view=start]] parenthesized expression {pre '{{lambda {:foo :bar} My first name is :foo and my last name is :bar. } James Bond} } _p where _ul {b lambda} stands for the verb {b replace}, more about later, _ul colon characters {b :} prefixing {b foo} and {b bar} are used instead of the blue color marking words to be replaced, _ul and a set of judiciously balanced {b curly braces} avoiding the need for the {b and}, {b in} and {b by} conjunctions. _p Given to my computer, this expression is replaced - {i in a magical way} - by {pre {{lambda {:foo :bar} My first name is :foo and my last name is :bar. } James Bond} } _p and it's what I am waiting for! _p The general form of such an expression is {pre '{{lambda {var ...} body } val ...} } _p and we will call it an "{b I}mmediately {b I}nvoked {b F}unction {b E}xpression", or {b [[IIFE|https://en.wikipedia.org/wiki/Immediately_invoked_function_expression]]}, following the name coined by [[Ben Alman|http://benalman.com/news/2010/11/immediately-invoked-function-expression/]] for javascript expressions such as {pre (function(foo,bar) '{ return "My first name is " + foo + "and my last name is " + bar +"." })("james","bond") } _p ... which does not shine by its great clarity! _p Because {b IIFE}s are rather convoluted and not easy to manage, lambdatalk comes with a useful syntactic sugar, {b let}. Thanks to this special form any {b IIFE} {pre '{{lambda {var ...} body} val ...} } _p can be replaced by the following one {pre '{let { {var val} ...} body} } _p enlighting the binding between {b local} variables and their values, for instance {pre '{let { {:foo james} {:bar bond} } My first name is :foo and my last name is :bar. } } _p to be compared with what we would write in javascript {pre let foo = "james", // define variables ... bar = "bond; // ... and give them a value return "My first name is " + foo + "and my last name is " + bar + "." } _p ... which is certainly more familiar, at least for the assignation part. Passing by, you will have noticed that sentences are less easy to write in javascript then in lambdatalk, which is more "text editor" oriented, texts and codes in this wiki page are entirely written and displayed in real time using the same lambdatalk syntax built on standard HTML, CSS, Maths, ... _p {b IIFE}s can be composed and for instance replacements in the initial example could be done in sequence {pre '{{lambda {:bar} {{lambda {:foo} My first name is :foo} James} and my last name is :bar.} Bond} -> {{lambda {:bar} {{lambda {:foo} My first name is :foo} James} and my last name is :bar.} Bond} } _p {b IIFE}s can be nested {b very deeply}. Let's consider this rather convoluted example {pre '{{{lambda {:g} {:g :g}} {lambda {:g :n :from :to :via} {{{lambda {:a :b :c} {:a :b :c}} {{lambda {:c} {:c {lambda {:a :b} {lambda {:a :b} :b}}}} :n} {lambda {:g :n :from :to :via} } {lambda {:g :n :from :to :via} {:g :g {{lambda {:c} {:c {lambda {:a :b} :b}}} :n} :from :via :to} {br} move {{lambda {:c} {:c {lambda {:a :b} :a}}} :n} from tower :from to tower :to {:g :g {{lambda {:c} {:c {lambda {:a :b} :b}}} :n} :via :to :from} }} :g :n :from :to :via}} } {{lambda {:a :b :c} {:c :a :b}} Disk_1 {{lambda {:a :b :c} {:c :a :b}} Disk_2 {{lambda {:a :b :c} {:c :a :b}} Disk_3 {{lambda {:a :b :c} {:c :a :b}} Disk_4 {{lambda {:a :b :c} {:c :a :b}} Disk_5 {lambda {:a} {lambda {:a :b} :a}}}}}}} A B C} } _p A frozen waterfall of lambdas, mixing code and data, {i homoiconicity} as they say. This IIFE is immediately evaluated to words, a list of sentences {pre {{{lambda {:g} {:g :g}} {lambda {:g :n :from :to :via} {{{lambda {:a :b :c} {:a :b :c}} {{lambda {:c} {:c {lambda {:a :b} {lambda {:a :b} :b}}}} :n} {lambda {:g :n :from :to :via} } {lambda {:g :n :from :to :via} {:g :g {{lambda {:c} {:c {lambda {:a :b} :b}}} :n} :from :via :to} {div} move {{lambda {:c} {:c {lambda {:a :b} :a}}} :n} from tower :from to tower :to {:g :g {{lambda {:c} {:c {lambda {:a :b} :b}}} :n} :via :to :from} }} :g :n :from :to :via}} } {{lambda {:a :b :c} {:c :a :b}} Disk_1 {{lambda {:a :b :c} {:c :a :b}} Disk_2 {{lambda {:a :b :c} {:c :a :b}} Disk_3 {{lambda {:a :b :c} {:c :a :b}} Disk_4 {{lambda {:a :b :c} {:c :a :b}} Disk_5 {lambda {:a} {lambda {:a :b} :a}}}}}}} A B C} } _p We just have resolved the game called "Towers of Hanoï" with three rods, A B C, and a set of 5 disks of decreasing size stacked on the first rod. The game consists in deplacing each disk to a second rod via a third one in respect of the following rule, "never put a disk on a smaller one ". _p Building such a {i frozen waterfall} of lambdas is explained elsewhere in the page [[coding]]. But in this page, we will just introduce two useful tools to write and read code more easily. _h2 functions and names _p Obviously {b IIFE}s are not easy to read and write and, in order to make things easier, we will split them into 3 steps: _ul 1) use the {b '{lambda {var} body}} expression as an {b anonymous function}, _ul 2) give it a {b name} inside a '{def name function} expression, _ul 3) and apply this function to values, {b (name values)}. _p And so, we split the initial {b IIFE} into _ul 1) a function called {b letmeintroducemyself} {pre '{def letmeintroducemyself {lambda {:foo :bar} My first name is :foo and my last name is :bar. }} -> {def letmeintroducemyself {lambda {:foo :bar} My first name is :foo and my last name is :bar. }} } _ul 2) and one or more invocations {pre '{letmeintroducemyself James Bond} -> {letmeintroducemyself James Bond} '{letmeintroducemyself Ward Cunningham} -> {letmeintroducemyself Ward Cunningham} } _p As a more useful example let's define a function swapping two words {pre '{def swap {lambda {:a :b} :b :a}} -> {def swap {lambda {:a :b} :b :a}} } _p and let's use it {pre '{swap James Bond} -> {swap James Bond} } _p At this point of the presentation we have built two user functions, {b letmeintroducemyself} & {b swap}, and we could go on, progressively populating a dictionary with functions dealing with booleans, data & control structures, numbers, ..., building a set of primitives, (+,-,*,/,sqrt,...), and some libraries. But it's another story to follow elsewhere, for instance in [[coding]]. _h2 the golden ratio _p But, wait, before we leave, {i ... just for fun}, this is an example using numbers, {i which are special cases of words}. Considering this [[continued fraction|https://en.wikipedia.org/wiki/Continued_fraction]] {pre 1 + 1 {{bt}1 + 1} {{bt}1 + 1} {{bt}1 + 1} {{bt}1 + ...} } _p which can be written as an infinite recurrent words rewritings {pre gold(0) = 1 gold(n) = 1 {{bt}1 + gold(n-1)} with n -> ∞ } _p and translated into a recursive algorithm {pre '{def gold {lambda {:n} {if {= :n 0} then 1 else {/ 1 {+ 1 {gold {- :n 1}}}} }}} -> {def gold {lambda {:n} {if {< :n 1} then 1 else {/ 1 {+ 1 {gold {- :n 1}}}}}}} } _p we can compute the golden ratio, {b φ} {pre '{+ 1 {gold 40}} -> {+ 1 {gold 40}} // which is φ = (1+√5)/2 } _p Nothing but a text replacement process, numbers are special cases of words. _h2 conclusion _p Beginning with a simple {b text rewriting} process called an IFFE, we have highlighted two fundamental processes working exclusively on {b words}, the {b abstraction} and the {b application}. Two concepts discovered in the thirties by Alonso Church under the name [[lambda_calculus]] - hence the strange name, {b lambda}, used for the verb {b replace} in the initial words replacement process, now you know why. _p Two twinned {b operators} probably also discovered centuries before by {b taoists} playing with {b Yin} and {b Yang}, the female and male principles, working on {b Verbs}. {b At the beginning was the Verb}. Why not? _p Welcome in the wonderful world of programming languages, a story you can read about in more details elsewhere, for instance in [[concepts]] or [[coding]]. _p {i Alain Marty (2021/09/01 - 2022/12/18)} | [[HN|https://news.ycombinator.com/item?id=34039631]] {blockquote {@ style="transform:rotate(-2deg); text-align:center; padding:10px;"} This wiki page is a lambdatalk program. } {{hide} {def bt span {@ style="border-top:1px solid;"}} } {style body, #page_frame, #page_content, .page_menu { font-size:1.1em; background:#fff; color:#000; border:0px solid #222; box-shadow:0 0 0 #444; } pre {box-shadow:0 0 8px #000; padding:5px; background:#eee; } b { color:#00f; } }
lambdaway v.20211111