First-class functions/Use numbers analogously: Difference between revisions
Content added Content deleted
(Added Scheme) |
m (Fixed lang tags.) |
||
Line 151: | Line 151: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
This new task: |
This new task: |
||
<lang python> |
<lang python>IDLE 2.6.1 |
||
IDLE 2.6.1 |
|||
>>> # Number literals |
>>> # Number literals |
||
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25 |
>>> x,xi, y,yi = 2.0,0.5, 4.0,0.25 |
||
Line 166: | Line 165: | ||
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)] |
>>> [multiplier(inversen, n)(.5) for n, inversen in zip(numlist, numlisti)] |
||
[0.5, 0.5, 0.5] |
[0.5, 0.5, 0.5] |
||
>>> |
>>></lang> |
||
The Python solution to First-class functions for comparison: |
The Python solution to First-class functions for comparison: |
||
<lang python> |
<lang python>>>> # Some built in functions and their inverses |
||
>>> # Some built in functions and their inverses |
|||
>>> from math import sin, cos, acos, asin |
>>> from math import sin, cos, acos, asin |
||
>>> # Add a user defined function and its inverse |
>>> # Add a user defined function and its inverse |
||
Line 184: | Line 182: | ||
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)] |
>>> [compose(inversef, f)(.5) for f, inversef in zip(funclist, funclisti)] |
||
[0.5, 0.4999999999999999, 0.5] |
[0.5, 0.4999999999999999, 0.5] |
||
>>> |
>>></lang> |
||
</lang> |
|||
As can be see, the treatment of functions is very close to the treatment of numbers. there are no extra wrappers, or function pointer syntax added, for example. |
As can be see, the treatment of functions is very close to the treatment of numbers. there are no extra wrappers, or function pointer syntax added, for example. |
||
Line 225: | Line 222: | ||
(2,000000 * 0,500000)(0,500000) = 0,500000 |
(2,000000 * 0,500000)(0,500000) = 0,500000 |
||
(4,000000 * 0,250000)(0,500000) = 0,500000 |
(4,000000 * 0,250000)(0,500000) = 0,500000 |
||
(6,000000 * 0,166667)(0,500000) = 0,500000 |
(6,000000 * 0,166667)(0,500000) = 0,500000</lang> |
||
</lang> |
|||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
Line 257: | Line 253: | ||
=={{header|Slate}}== |
=={{header|Slate}}== |
||
⚫ | |||
<lang slate> |
|||
⚫ | |||
define: #x -> 2. |
define: #x -> 2. |
||
define: #y -> 4. |
define: #y -> 4. |
||
Line 264: | Line 259: | ||
define: #numlisti -> (numlist collect: [| :x | 1.0 / x]). |
define: #numlisti -> (numlist collect: [| :x | 1.0 / x]). |
||
numlist with: numlisti collect: [| :n1 :n2 | (multiplier applyTo: {n1. n2}) applyWith: 0.5]. |
numlist with: numlisti collect: [| :n1 :n2 | (multiplier applyTo: {n1. n2}) applyWith: 0.5].</lang> |
||
</lang> |
|||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 300: | Line 294: | ||
composition operator (+), and is named in compliance |
composition operator (+), and is named in compliance |
||
with the task specification. |
with the task specification. |
||
<lang Ursala> |
<lang Ursala>#import std |
||
#import std |
|||
#import flo |
#import flo |
||
Line 311: | Line 304: | ||
#cast %eL |
#cast %eL |
||
main = (gang multiplier*p\numbers inverses) 0.5 |
main = (gang multiplier*p\numbers inverses) 0.5</lang> |
||
</lang> |
|||
The multiplier could have been written in pattern |
The multiplier could have been written in pattern |
||
matching form like this. |
matching form like this. |
||
⚫ | |||
<lang Ursala> |
|||
⚫ | |||
</lang> |
|||
The main program might also have been written with an |
The main program might also have been written with an |
||
anonymous function like this. |
anonymous function like this. |
||
⚫ | |||
<lang Ursala> |
|||
⚫ | |||
</lang> |
|||
output: |
output: |
||
<pre> |
<pre> |