Jump to content

Y combinator: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(add task to ARM64 assembly Raspberry Pi)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 16:
* [http://vimeo.com/45140590 Jim Weirich: Adventures in Functional Programming]
<br><br>
 
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
Line 704 ⟶ 705:
Program normal end.
</pre>
 
=={{header|ATS}}==
<lang ATS>
Line 1,976 ⟶ 1,978:
<lang lisp>(defn Y [f]
(#(% %) #(f (fn [& args] (apply (% %) args)))))</lang>
 
=={{header|CoffeeScript}}==
<lang coffeescript>Y = (f) -> g = f( (t...) -> g(t...) )</lang>
or
<lang coffeescript>Y = (f) -> ((h)->h(h))((h)->f((t...)->h(h)(t...)))</lang>
<lang coffeescript>fac = Y( (f) -> (n) -> if n > 1 then n * f(n-1) else 1 )
fib = Y( (f) -> (n) -> if n > 1 then f(n-1) + f(n-2) else n )
</lang>
 
=={{header|Common Lisp}}==
Line 2,007 ⟶ 2,017:
? (mapcar #'fib '(1 2 3 4 5 6 7 8 9 10))
(1 1 2 3 5 8 13 21 34 55)</lang>
 
=={{header|CoffeeScript}}==
<lang coffeescript>Y = (f) -> g = f( (t...) -> g(t...) )</lang>
or
<lang coffeescript>Y = (f) -> ((h)->h(h))((h)->f((t...)->h(h)(t...)))</lang>
<lang coffeescript>fac = Y( (f) -> (n) -> if n > 1 then n * f(n-1) else 1 )
fib = Y( (f) -> (n) -> if n > 1 then f(n-1) + f(n-2) else n )
</lang>
 
=={{header|D}}==
Line 2,046 ⟶ 2,048:
<pre>factorial: [1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]
ackermann(3, 5): 253</pre>
 
=={{header|Déjà Vu}}==
{{trans|Python}}
<lang dejavu>Y f:
labda y:
labda:
call y @y
f
labda x:
x @x
call
 
labda f:
labda n:
if < 1 n:
* n f -- n
else:
1
set :fac Y
 
labda f:
labda n:
if < 1 n:
+ f - n 2 f -- n
else:
1
set :fib Y
 
!. fac 6
!. fib 6</lang>
{{out}}
<pre>720
13</pre>
 
=={{header|Delphi}}==
Line 2,152 ⟶ 2,121:
Writeln ('Fac(10) = ', Fac (10));
end.</lang>
 
=={{header|Déjà Vu}}==
{{trans|Python}}
<lang dejavu>Y f:
labda y:
labda:
call y @y
f
labda x:
x @x
call
 
labda f:
labda n:
if < 1 n:
* n f -- n
else:
1
set :fac Y
 
labda f:
labda n:
if < 1 n:
+ f - n 2 f -- n
else:
1
set :fib Y
 
!. fac 6
!. fib 6</lang>
{{out}}
<pre>720
13</pre>
 
=={{header|E}}==
Line 2,239 ⟶ 2,241:
{{out}}
<pre>(479001600,144)</pre>
 
=={{header|Elena}}==
{{trans|Smalltalk}}
Line 2,444 ⟶ 2,447:
Unit Test: { [ 120 ] [ 5 [ almost-fac ] Y call ] }
Unit Test: { [ 8 ] [ 6 [ almost-fib ] Y call ] }</pre>
 
=={{header|Falcon}}==
<lang Falcon>
Y = { f => {x=> {n => f(x(x))(n)}} ({x=> {n => f(x(x))(n)}}) }
facStep = { f => {x => x < 1 ? 1 : x*f(x-1) }}
fibStep = { f => {x => x == 0 ? 0 : (x == 1 ? 1 : f(x-1) + f(x-2))}}
 
YFac = Y(facStep)
YFib = Y(fibStep)
 
> "Factorial 10: ", YFac(10)
> "Fibonacci 10: ", YFib(10)
</lang>
 
=={{header|Forth}}==
Line 2,467 ⟶ 2,483:
10 :noname ( u1 xt -- u2 ) over 2 < if drop else >r 1- dup r@ execute swap 1- r> execute + then ;
y execute . 55 ok
</lang>
 
=={{header|Falcon}}==
<lang Falcon>
Y = { f => {x=> {n => f(x(x))(n)}} ({x=> {n => f(x(x))(n)}}) }
facStep = { f => {x => x < 1 ? 1 : x*f(x-1) }}
fibStep = { f => {x => x == 0 ? 0 : (x == 1 ? 1 : f(x-1) + f(x-2))}}
 
YFac = Y(facStep)
YFib = Y(fibStep)
 
> "Factorial 10: ", YFac(10)
> "Fibonacci 10: ", YFib(10)
</lang>
 
Line 3,381 ⟶ 3,384:
factorial, fibs = Y(almostfactorial), Y(almostfibs)
print(factorial(7))</lang>
 
 
=={{header|M2000 Interpreter}}==
Line 3,719 ⟶ 3,721:
sub {$f->(Y($f))->(@_)}
}</lang>
 
=={{header|Perl 6}}==
<lang perl6>sub Y (&f) { sub (&x) { x(&x) }( sub (&y) { f(sub ($x) { y(&y)($x) }) } ) }
sub fac (&f) { sub ($n) { $n < 2 ?? 1 !! $n * f($n - 1) } }
sub fib (&f) { sub ($n) { $n < 2 ?? $n !! f($n - 1) + f($n - 2) } }
say map Y($_), ^10 for &fac, &fib;</lang>
{{out}}
<pre>(1 1 2 6 24 120 720 5040 40320 362880)
(0 1 1 2 3 5 8 13 21 34)</pre>
 
Note that Perl 6 doesn't actually need a Y combinator because you can name anonymous functions from the inside:
 
<lang perl6>say .(10) given sub (Int $x) { $x < 2 ?? 1 !! $x * &?ROUTINE($x - 1); }</lang>
 
=={{header|Phix}}==
Line 4,209 ⟶ 4,198:
 
(fact 5)</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>sub Y (&f) { sub (&x) { x(&x) }( sub (&y) { f(sub ($x) { y(&y)($x) }) } ) }
sub fac (&f) { sub ($n) { $n < 2 ?? 1 !! $n * f($n - 1) } }
sub fib (&f) { sub ($n) { $n < 2 ?? $n !! f($n - 1) + f($n - 2) } }
say map Y($_), ^10 for &fac, &fib;</lang>
{{out}}
<pre>(1 1 2 6 24 120 720 5040 40320 362880)
(0 1 1 2 3 5 8 13 21 34)</pre>
 
Note that Perl 6 doesn't actually need a Y combinator because you can name anonymous functions from the inside:
 
<lang perl6>say .(10) given sub (Int $x) { $x < 2 ?? 1 !! $x * &?ROUTINE($x - 1); }</lang>
 
=={{header|REBOL}}==
Line 4,605 ⟶ 4,608:
The usual version using recursion, disallowed by the task:
<lang sml>fun fix f x = f (fix f) x</lang>
 
 
=={{header|SuperCollider}}==
Line 4,857 ⟶ 4,859:
fib
1 1 2 3 5 8 13 21 34 55 </pre>
 
=={{header|Verbexx}}==
<lang verbexx>/////// Y-combinator function (for single-argument lambdas) ///////
10,349

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.