Currying: Difference between revisions
m
→{{header|Binary Lambda Calculus}}
(Dialects of BASIC moved to the BASIC section.) |
|||
(10 intermediate revisions by 8 users not shown) | |||
Line 464:
bc
bcde</pre>
=={{header|Binary Lambda Calculus}}==
In BLC, all multi argument functions are necessarily achieved by currying, since lambda calculus functions (lambdas) are single argument. A good example is the Church numeral 2, which given a function f and an argument x, applies f twice on x: C2 = \f. (\x. f (f x)). This is written in BLC as
<pre>00 00 01 110 01 110 01</pre>
where 00 denotes lambda, 01 denotes application, and 1^n0 denotes the variable bound by the n'th enclosing lambda. Which is all there is to BCL!
=={{header|BQN}}==
Line 637 ⟶ 645:
=={{header|EchoLisp}}==
[[EchoLisp]] has native support for curry, which is implemented thru closures, as shown in [[
<syntaxhighlight lang="text">
;;
Line 662 ⟶ 670:
=={{header|Ecstasy}}==
<syntaxhighlight lang="java">
module CurryPower {
@Inject Console console;
void run() {
function Int(Int, Int) divide = (x,y) -> x / y;
function Int(
function Int(Int) partsOf120 = divide(120, _);
console.print($|half of a dozen is {half(12)}
Line 677 ⟶ 682:
|and a quarter is {partsOf120(4)}
);
}
}
</syntaxhighlight>
Line 732 ⟶ 737:
where FUNCTION [ANY, TUPLE [Y], Z] denotes the type ''Y'' → ''Z'' (agents taking as argument a tuple with a single argument of type Y and returning a result of type Z), which is indeed the type of the agent expression used on the next-to-last line to define the "Result" of g.
=={{header|Elixir}}==
<pre>
iex(1)> plus = fn x, y -> x + y end
#Function<41.125776118/2 in :erl_eval.expr/6>
iex(2)> plus.(3, 5)
8
iex(3)> plus5 = &plus.(5, &1)
#Function<42.125776118/1 in :erl_eval.expr/6>
iex(4)> plus5.(3)
8
</pre>
=={{header|EMal}}==
Line 922 ⟶ 940:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Currying}}
'''Solution'''
In Fōrmulæ, a function is just a named lambda expression, and a function call is just a lambda application.
The following is a simple definition of a lambda expression:
[[File:Fōrmulæ - Currying 01.png]]
When a lambda application is called with the same number of arguments, the result is the habitual:
[[File:Fōrmulæ - Currying 02.png]]
[[File:Fōrmulæ - Currying 03.png]]
However, if a less number of parameters is applied, currying is performed. Notice that the result is another lambda expression.
[[File:Fōrmulæ - Currying 04.png]]
[[File:Fōrmulæ - Currying 05.png]]
Because the result is a lambda expression, it can be used in a lambda application, so we must get the same result:
[[File:Fōrmulæ - Currying 06.png]]
[[File:Fōrmulæ - Currying 03.png]]
Using functions:
[[File:Fōrmulæ - Currying 07.png]]
[[File:Fōrmulæ - Currying 08.png]]
=={{header|GDScript}}==
{{trans|Python}}
Uses Godot 4's lambdas. This runs as a script attached to a node.
<syntaxhighlight lang="gdscript">
extends Node
func addN(n: int) -> Callable:
return func(x):
return n + x
func _ready():
# Test currying
var add2 := addN(2)
print(add2.call(7))
get_tree().quit() # Exit
</syntaxhighlight>
=={{header|Go}}==
Line 1,410 ⟶ 1,475:
</pre>
A shorter form of the above function, also without type specification:
<syntaxhighlight lang="julia">
addN(n) = x -> n + x
</syntaxhighlight>
=={{header|Kotlin}}==
Line 1,651 ⟶ 1,721:
Out[5]:= 5
</pre>
=={{header|MiniScript}}==
{{trans|Rust}}
<syntaxhighlight lang="miniscript">addN = function(n)
f = function(x)
return n + x
end function
return @f
end function
adder = addN(40)
print "The answer to life is " + adder(2) + "."</syntaxhighlight>
{{out}}
<pre>The answer to life is 42.</pre>
=={{header|Nemerle}}==
Line 2,394 ⟶ 2,479:
=={{header|Wren}}==
{{trans|Rust}}
<syntaxhighlight lang="
var adder = addN.call(40)
|