Currying: Difference between revisions

1,955 bytes added ,  3 months ago
m
 
(6 intermediate revisions by 5 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 [[CommonLisp#Common_Lisp|Common Lisp]] .
<syntaxhighlight lang="text">
;;
Line 729 ⟶ 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 920 ⟶ 941:
 
{{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}}==
Line 1,668 ⟶ 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,411 ⟶ 2,479:
=={{header|Wren}}==
{{trans|Rust}}
<syntaxhighlight lang="ecmascriptwren">var addN = Fn.new { |n| Fn.new { |x| n + x } }
 
var adder = addN.call(40)
56

edits