Call a function: Difference between revisions
Content added Content deleted
(adding a contribution with lambdatalk) |
|||
Line 2,714: | Line 2,714: | ||
Hello world |
Hello world |
||
</pre> |
</pre> |
||
=={{header|Lambdatalk}}== |
|||
In lambdatalk functions are abstractions {lambda {args} body} whose behaviour is best explained as a part of such a complete expression {{lambda {args} body} values}. |
|||
<lang scheme> |
|||
The command |
|||
replace :a0 :a1 ... an-1 |
|||
in expression containing some occurences of :ai |
|||
by v0 v1 ... vp-1 |
|||
is rewritten in a prefixed parenthesized form |
|||
{{lambda {:a0 :a1 ... an-1} |
|||
expression containing some occurences of :ai} |
|||
v0 v1 ... vp-1} |
|||
so called IIFE (Immediately Invoked Function Expression), and defines an anonymous function containing a sequence of n arguments :ai, immediately invoked on a sequence of p values vi, and returning the expression in its body as so modified: |
|||
1) if p < n (partial application) |
|||
• the occurrences of the p first arguments are replaced in the function's body by the corresponding p given values, |
|||
• a function waiting for missing n-p values is created, |
|||
• and its reference is returned. |
|||
• example: |
|||
{{lambda {:x :y} ... :y ... :x ...} hello} |
|||
-> {lambda {:y} ... :y ... hello ...} // replaces :x by hello |
|||
-> LAMB_123 // the new functions's reference |
|||
• called with the value world this function will return ... world ... hello ... |
|||
2) if p = n (normal application) |
|||
• the occurences of the n arguments are replaced in the function's body by the corresponding p given values, |
|||
• the body is evaluated and the result is returned. |
|||
• example |
|||
{{lambda {:x :y} ... :y ... :x ...} hello world} |
|||
-> {{lambda {:y} ... :y ... hello ...} world} // replaces :x by hello |
|||
-> {{lambda {} ... world ... hello ...} } // replaces :y by world |
|||
-> ... world ... hello ... // the value |
|||
3) if p > n (variadicity) |
|||
• the occurrences of the n-1 first arguments are replaced in the function's body by the corresponding n-1 given values, |
|||
• the occurrences of the last argument are replaced in the body by the sequence of p-n supernumerary values, |
|||
• the body is evaluated and the result is returned. |
|||
• example: |
|||
{{lambda {:x :y} ... :y ... :x ...} hello world good morning} |
|||
-> {{lambda {:y} ... :y ... hello ...} world good morning} |
|||
-> {{lambda {} ... world good morning ... hello ...}} |
|||
-> ... world good morning ... hello ... // the value |
|||
More can be seen in http://lambdaway.free.fr/lambdawalks/?view=lambda |
|||
</lang> |
|||
=={{header|langur}}== |
=={{header|langur}}== |