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