Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
18
Task/Mutual-recursion/ALGOL-W/mutual-recursion.alg
Normal file
18
Task/Mutual-recursion/ALGOL-W/mutual-recursion.alg
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
begin
|
||||
% define mutually recursive funtions F and M that compute the elements %
|
||||
% of the Hofstadter Female and Male sequences %
|
||||
|
||||
integer procedure F ( integer value n ) ;
|
||||
if n = 0 then 1 else n - M( F( n - 1 ) );
|
||||
|
||||
integer procedure M ( integer value n ) ;
|
||||
if n = 0 then 0 else n - F( M( n - 1 ) );
|
||||
|
||||
% print the first few elements of the sequences %
|
||||
i_w := 2; s_w := 1; % set I/O formatting %
|
||||
write( "F: " );
|
||||
for i := 0 until 20 do writeon( F( i ) );
|
||||
write( "M: " );
|
||||
for i := 0 until 20 do writeon( M( i ) );
|
||||
|
||||
end.
|
||||
47
Task/Mutual-recursion/Eiffel/mutual-recursion.e
Normal file
47
Task/Mutual-recursion/Eiffel/mutual-recursion.e
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
class
|
||||
APPLICATION
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature
|
||||
|
||||
make
|
||||
-- Test of the mutually recursive functions Female and Male.
|
||||
do
|
||||
across
|
||||
0 |..| 19 as c
|
||||
loop
|
||||
io.put_string (Female (c.item).out + " ")
|
||||
end
|
||||
io.new_line
|
||||
across
|
||||
0 |..| 19 as c
|
||||
loop
|
||||
io.put_string (Male (c.item).out + " ")
|
||||
end
|
||||
end
|
||||
|
||||
Female (n: INTEGER): INTEGER
|
||||
-- Female sequence of the Hofstadter Female and Male sequences.
|
||||
require
|
||||
n_not_negative: n >= 0
|
||||
do
|
||||
Result := 1
|
||||
if n /= 0 then
|
||||
Result := n - Male (Female (n - 1))
|
||||
end
|
||||
end
|
||||
|
||||
Male (n: INTEGER): INTEGER
|
||||
-- Male sequence of the Hofstadter Female and Male sequences.
|
||||
require
|
||||
n_not_negative: n >= 0
|
||||
do
|
||||
Result := 0
|
||||
if n /= 0 then
|
||||
Result := n - Female (Male (n - 1))
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -4,3 +4,6 @@ defmodule MutualRecursion do
|
|||
def m(0), do: 0
|
||||
def m(n), do: n - f(m(n - 1))
|
||||
end
|
||||
|
||||
IO.inspect Enum.map(0..19, fn n -> MutualRecursion.f(n) end)
|
||||
IO.inspect Enum.map(0..19, fn n -> MutualRecursion.m(n) end)
|
||||
|
|
|
|||
19
Task/Mutual-recursion/JavaScript/mutual-recursion-1.js
Normal file
19
Task/Mutual-recursion/JavaScript/mutual-recursion-1.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
function f(num) {
|
||||
return (num === 0) ? 1 : num - m(f(num - 1));
|
||||
}
|
||||
|
||||
function m(num) {
|
||||
return (num === 0) ? 0 : num - f(m(num - 1));
|
||||
}
|
||||
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(
|
||||
function (x, i) { return m + i; }
|
||||
);
|
||||
}
|
||||
|
||||
var a = range(0, 19);
|
||||
|
||||
//return a new array of the results and join with commas to print
|
||||
console.log(a.map(function (n) { return f(n); }).join(', '));
|
||||
console.log(a.map(function (n) { return m(n); }).join(', '));
|
||||
14
Task/Mutual-recursion/JavaScript/mutual-recursion-2.js
Normal file
14
Task/Mutual-recursion/JavaScript/mutual-recursion-2.js
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
var f = num => (num === 0) ? 1 : num - m(f(num - 1));
|
||||
var m = num => (num === 0) ? 0 : num - f(m(num - 1));
|
||||
|
||||
function range(m, n) {
|
||||
return Array.apply(null, Array(n - m + 1)).map(
|
||||
function (x, i) { return m + i; }
|
||||
);
|
||||
}
|
||||
|
||||
var a = range(0, 19);
|
||||
|
||||
//return a new array of the results and join with commas to print
|
||||
console.log(a.map(n => f(n)).join(', '));
|
||||
console.log(a.map(n => m(n)).join(', '));
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
function F(n)
|
||||
{
|
||||
return n === 0 ? 1 : n - M(F(n - 1));
|
||||
}
|
||||
|
||||
function M(n)
|
||||
{
|
||||
return n === 0 ? 0 : n - F(M(n - 1));
|
||||
}
|
||||
|
||||
var
|
||||
out = {F: [], M: []},
|
||||
i;
|
||||
for (i = 0; i < 20; i++)
|
||||
{
|
||||
out.F.push(F(i));
|
||||
out.M.push(M(i));
|
||||
}
|
||||
print(out.F + "\n" + out.M);
|
||||
|
|
@ -1,11 +1,10 @@
|
|||
/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
|
||||
parse arg lim .; if lim='' then lim=40; pad=left('',20)
|
||||
/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
|
||||
parse arg lim .; if lim='' then lim=40; w=length(lim); pad=left('',20)
|
||||
|
||||
do j=0 to lim; jj=Jw(j); ff=F(j); mm=M(j)
|
||||
say pad 'F('jj") =" Jw(ff) pad 'M('jj") =" Jw(mm)
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*─────────────────────────────────────F, M, Jw subroutines────────────*/
|
||||
F: procedure; parse arg n; if n==0 then return 1; return n-M(F(n-1))
|
||||
M: procedure; parse arg n; if n==0 then return 0; return n-F(M(n-1))
|
||||
Jw: return right(arg(1),length(lim)) /*right justifies # for nice look*/
|
||||
do j=0 to lim; jj=right(j,w); ff=right(F(j),w); mm=right(M(j),w)
|
||||
say pad 'F('jj") =" ff pad 'M('jj") =" mm
|
||||
end /*j*/
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*────────────────────────────────────────────────────────────────────────────*/
|
||||
F: procedure; parse arg n; if n==0 then return 1; return n - M(F(n-1))
|
||||
M: procedure; parse arg n; if n==0 then return 0; return n - F(M(n-1))
|
||||
|
|
|
|||
|
|
@ -1,15 +1,14 @@
|
|||
/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
|
||||
parse arg lim .; if lim=='' then lim=99 /*get or assume LIM.*/
|
||||
hm.=; hm.0=0; hf.=; hf.0=1; Js=; Fs=; Ms=
|
||||
/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
|
||||
parse arg lim .; if lim=='' then lim=40 /*assume the default for LIM? */
|
||||
w=length(lim); $m.=.; $m.0=0; $f.=.; $f.0=1; Js=; Fs=; Ms=
|
||||
|
||||
do j=0 to lim; ff=F(j); mm=M(j)
|
||||
Js=Js jW(j); Fs=Fs jw(ff); Ms=Ms jW(mm)
|
||||
end /*j*/
|
||||
say 'Js=' Js
|
||||
say 'Fs=' Fs
|
||||
say 'Ms=' Ms
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────one─liner subroutines──────────────────────────────*/
|
||||
F: procedure expose hm. hf.; parse arg n; if hf.n=='' then hf.n=n-M(F(n-1)); return hf.n
|
||||
M: procedure expose hm. hf.; parse arg n; if hm.n=='' then hm.n=n-F(M(n-1)); return hm.n
|
||||
Jw: return right(arg(1),length(lim)) /*right justifies # for nice look*/
|
||||
do j=0 to lim
|
||||
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(j),w)
|
||||
end /*j*/
|
||||
say 'Js=' Js /*display the list of Js to the term.*/
|
||||
say 'Fs=' Fs /* " " " " Fs " " " */
|
||||
say 'Ms=' Ms /* " " " " Ms " " " */
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*───────────────────────────────────────────────────────────────────────────────────*/
|
||||
F: procedure expose $m. $f.; parse arg n; if $f.n==. then $f.n=n-M(F(n-1)); return $f.n
|
||||
M: procedure expose $m. $f.; parse arg n; if $m.n==. then $m.n=n-F(M(n-1)); return $m.n
|
||||
|
|
|
|||
|
|
@ -1,18 +1,17 @@
|
|||
/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
|
||||
/*If LIM is negative, only show a single result for the abs(lim) entry.*/
|
||||
/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
|
||||
/*────────If LIM is negative, a single result is shown for the abs(lim) entry.*/
|
||||
|
||||
parse arg lim .; if lim=='' then lim=99; aLim=abs(lim)
|
||||
parse var lim . hm. hf. Js Fs Ms; hm.0=0; hf.0=1
|
||||
parse arg lim .; if lim=='' then lim=99; aLim=abs(lim)
|
||||
w=length(aLim); $m.=.; $m.0=0; $f.=.; $f.0=1; Js=; Fs=; Ms=
|
||||
|
||||
do j=0 to Alim; ff=F(j); mm=M(j)
|
||||
Js=Js jW(j); Fs=Fs jw(ff); Ms=Ms jW(mm)
|
||||
end
|
||||
do j=0 to Alim
|
||||
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(j),w)
|
||||
end /*j*/
|
||||
|
||||
if lim>0 then say 'Js=' Js; else say 'J('aLim")=" word(Js,aLim+1)
|
||||
if lim>0 then say 'Fs=' Fs; else say 'F('aLim")=" word(Fs,aLim+1)
|
||||
if lim>0 then say 'Ms=' Ms; else say 'M('aLim")=" word(Ms,aLim+1)
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────one─liner subroutines──────────────────────────────*/
|
||||
F: procedure expose hm. hf.; parse arg n; if hf.n=='' then hf.n=n-M(F(n-1)); return hf.n
|
||||
M: procedure expose hm. hf.; parse arg n; if hm.n=='' then hm.n=n-F(M(n-1)); return hm.n
|
||||
Jw: return right(arg(1),length(lim)) /*right justifies # for nice look*/
|
||||
if lim>0 then say 'Js=' Js; else say 'J('aLim")=" word(Js,aLim+1)
|
||||
if lim>0 then say 'Fs=' Fs; else say 'F('aLim")=" word(Fs,aLim+1)
|
||||
if lim>0 then say 'Ms=' Ms; else say 'M('aLim")=" word(Ms,aLim+1)
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*───────────────────────────────────────────────────────────────────────────────────*/
|
||||
F: procedure expose $m. $f.; parse arg n; if $f.n==. then $f.n=n-M(F(n-1)); return $f.n
|
||||
M: procedure expose $m. $f.; parse arg n; if $m.n==. then $m.n=n-F(M(n-1)); return $m.n
|
||||
|
|
|
|||
|
|
@ -1,11 +1,11 @@
|
|||
fn f(n: int) -> int {
|
||||
fn f(n: u32) -> u32 {
|
||||
match n {
|
||||
0 => 1,
|
||||
_ => n - m(f(n - 1))
|
||||
}
|
||||
}
|
||||
|
||||
fn m(n: int) -> int {
|
||||
fn m(n: u32) -> u32 {
|
||||
match n {
|
||||
0 => 0,
|
||||
_ => n - f(m(n - 1))
|
||||
|
|
@ -13,12 +13,12 @@ fn m(n: int) -> int {
|
|||
}
|
||||
|
||||
fn main() {
|
||||
for i in range(0, 20).map(f) {
|
||||
for i in (0..20).map(f) {
|
||||
print!("{} ", i);
|
||||
}
|
||||
println!("")
|
||||
println!("");
|
||||
|
||||
for i in range(0, 20).map(m) {
|
||||
for i in (0..20).map(m) {
|
||||
print!("{} ", i);
|
||||
}
|
||||
println!("")
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue