This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,12 @@
Two functions are said to be mutually recursive if the first calls the second, and in turn the second calls the first.
Write two mutually recursive functions that compute members of the [[wp:Hofstadter sequence#Hofstadter Female and Male sequences|Hofstadter Female and Male sequences]] defined as:
:<math>
\begin{align}
F(0)&=1\ ;\ M(0)=0 \\
F(n)&=n-M(F(n-1)), \quad n>0 \\
M(n)&=n-F(M(n-1)), \quad n>0.
\end{align}
</math>
<br>(If a language does not allow for a solution using mutually recursive functions then state this rather than give a solution by other means).

View file

@ -0,0 +1,2 @@
---
note: recursion

View file

@ -0,0 +1,12 @@
(mutual-recursion
(defun f (n)
(declare (xargs :mode :program))
(if (zp n)
1
(- n (m (f (1- n))))))
(defun m (n)
(declare (xargs :mode :program))
(if (zp n)
0
(- n (f (m (1- n)))))))

View file

@ -0,0 +1,21 @@
PROC (INT)INT m; # ONLY required for ELLA ALGOL 68RS - an official subset OF full ALGOL 68 #
PROC f = (INT n)INT:
IF n = 0 THEN 1
ELSE n - m(f(n-1)) FI;
m := (INT n)INT:
IF n = 0 THEN 0
ELSE n - f(m(n-1)) FI;
main:
(
FOR i FROM 0 TO 19 DO
print(whole(f(i),-3))
OD;
new line(stand out);
FOR i FROM 0 TO 19 DO
print(whole(m(i),-3))
OD;
new line(stand out)
)

View file

@ -0,0 +1,22 @@
function F(n)
{
if ( n == 0 ) return 1;
return n - M(F(n-1))
}
function M(n)
{
if ( n == 0 ) return 0;
return n - F(M(n-1))
}
BEGIN {
for(i=0; i < 20; i++) {
printf "%3d ", F(i)
}
print ""
for(i=0; i < 20; i++) {
printf "%3d ", M(i)
}
print ""
}

View file

@ -0,0 +1,28 @@
with Ada.Text_Io; use Ada.Text_Io;
procedure Mutual_Recursion is
function M(N : Integer) return Integer;
function F(N : Integer) return Integer is
begin
if N = 0 then
return 1;
else
return N - M(F(N - 1));
end if;
end F;
function M(N : Integer) return Integer is
begin
if N = 0 then
return 0;
else
return N - F(M(N-1));
end if;
end M;
begin
for I in 0..19 loop
Put_Line(Integer'Image(F(I)));
end loop;
New_Line;
for I in 0..19 loop
Put_Line(Integer'Image(M(I)));
end loop;
end Mutual_recursion;

View file

@ -0,0 +1,42 @@
integer F(integer n);
integer M(integer n);
integer F(integer n)
{
integer r;
if (n) {
r = n - M(F(n - 1));
} else {
r = 1;
}
return r;
}
integer M(integer n)
{
integer r;
if (n) {
r = n - F(M(n - 1));
} else {
r = 0;
}
return r;
}
integer main(void)
{
integer i;
i = 0;
while (i < 20) {
o_winteger(3, F(i));
i += 1;
}
o_byte('\n');
i = 0;
while (i < 20) {
o_winteger(3, M(i));
i += 1;
}
o_byte('\n');
return 0;
}

View file

@ -0,0 +1,11 @@
Loop 20
i := A_Index-1, t .= "`n" i "`t " M(i) "`t " F(i)
MsgBox x`tmale`tfemale`n%t%
F(n) {
Return n ? n - M(F(n-1)) : 1
}
M(n) {
Return n ? n - F(M(n-1)) : 0
}

View file

@ -0,0 +1,31 @@
main()
Return
F(n)
{
If (n == 0)
Return 1
Else
Return n - M(F(n-1))
}
M(n)
{
If (n == 0)
Return 0
Else
Return n - F(M(n-1)) ;
}
main()
{
i = 0
While, i < 20
{
male .= M(i) . "`n"
female .= F(i) . "`n"
i++
}
MsgBox % "male:`n" . male
MsgBox % "female:`n" . female
}

View file

@ -0,0 +1,18 @@
DECLARE FUNCTION f! (n!)
DECLARE FUNCTION m! (n!)
FUNCTION f! (n!)
IF n = 0 THEN
f = 1
ELSE
f = m(f(n - 1))
END IF
END FUNCTION
FUNCTION m! (n!)
IF n = 0 THEN
m = 0
ELSE
m = f(m(n - 1))
END IF
END FUNCTION

View file

@ -0,0 +1,16 @@
@% = 3 : REM Column width
PRINT "F sequence:"
FOR i% = 0 TO 20
PRINT FNf(i%) ;
NEXT
PRINT
PRINT "M sequence:"
FOR i% = 0 TO 20
PRINT FNm(i%) ;
NEXT
PRINT
END
DEF FNf(n%) IF n% = 0 THEN = 1 ELSE = n% - FNm(FNf(n% - 1))
DEF FNm(n%) IF n% = 0 THEN = 0 ELSE = n% - FNf(FNm(n% - 1))

View file

@ -0,0 +1,9 @@
define f(n) {
if ( n == 0 ) return(1);
return(n - m(f(n-1)));
}
define m(n) {
if ( n == 0 ) return(0);
return(n - f(m(n-1)));
}

View file

@ -0,0 +1,9 @@
/* GNU bc */
for(i=0; i < 19; i++) {
print f(i); print " ";
}
print "\n";
for(i=0; i < 19; i++) {
print m(i); print " ";
}
print "\n";

View file

@ -0,0 +1,8 @@
(F=.!arg:0&1|!arg+-1*M$(F$(!arg+-1)));
(M=.!arg:0&0|!arg+-1*F$(M$(!arg+-1)));
-1:?n&whl'(!n+1:~>20:?n&put$(F$!n " "))&put$\n
1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13
-1:?n&whl'(!n+1:~>20:?n&put$(M$!n " "))&put$\n
0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12

View file

@ -0,0 +1,16 @@
female = null #yes, this is necessary
male = { n |
true? n == 0
{ 0 }
{ n - female male(n - 1) }
}
female = { n |
true? n == 0
{ 1 }
{ n - male female(n - 1 ) }
}
p 0.to(20).map! { n | female n }
p 0.to(20).map! { n | male n }

View file

@ -0,0 +1,36 @@
#include <iostream>
#include <vector>
#include <iterator>
class Hofstadter
{
public:
static int F(int n) {
if ( n == 0 ) return 1;
return n - M(F(n-1));
}
static int M(int n) {
if ( n == 0 ) return 0;
return n - F(M(n-1));
}
};
using namespace std;
int main()
{
int i;
vector<int> ra, rb;
for(i=0; i < 20; i++) {
ra.push_back(Hofstadter::F(i));
rb.push_back(Hofstadter::M(i));
}
copy(ra.begin(), ra.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
copy(rb.begin(), rb.end(),
ostream_iterator<int>(cout, " "));
cout << endl;
return 0;
}

View file

@ -0,0 +1,18 @@
class Hofstadter
{
public:
static int F(int n);
static int M(int n);
};
int Hofstadter::F(int n)
{
if ( n == 0 ) return 1;
return n - M(F(n-1));
}
int Hofstadter::M(int n)
{
if ( n == 0 ) return 0;
return n - F(M(n-1));
}

View file

@ -0,0 +1,30 @@
#include <stdio.h>
#include <stdlib.h>
/* let us declare our functions; indeed here we need
really only M declaration, so that F can "see" it
and the compiler won't complain with a warning */
int F(const int n);
int M(const int n);
int F(const int n)
{
return (n == 0) ? 1 : n - M(F(n - 1));
}
int M(const int n)
{
return (n == 0) ? 0 : n - F(M(n - 1));
}
int main(void)
{
int i;
for (i = 0; i < 20; i++)
printf("%2d ", F(i));
printf("\n");
for (i = 0; i < 20; i++)
printf("%2d ", M(i));
printf("\n");
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,11 @@
(declare F) ; forward reference
(defn M [n]
(if (zero? n)
0
(- n (F (M (dec n))))))
(defn F [n]
(if (zero? n)
1
(- n (M (F (dec n))))))

View file

@ -0,0 +1,8 @@
F = (n) ->
if n is 0 then 1 else n - M F n - 1
M = (n) ->
if n is 0 then 0 else n - F M n - 1
console.log [0...20].map F
console.log [0...20].map M

View file

@ -0,0 +1,3 @@
> coffee mutual_recurse.coffee
[ 1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12 ]
[ 0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12 ]

View file

@ -0,0 +1,9 @@
(defun m (n)
(if (zerop n)
0
(- n (f (m (- n 1))))))
(defun f (n)
(if (zerop n)
1
(- n (m (f (- n 1))))))

View file

@ -0,0 +1,14 @@
import std.stdio, std.algorithm, std.range;
/*auto*/ int male(in int n) pure nothrow {
return n ? (n - female(male(n - 1))) : 0;
}
/*auto*/ int female(in int n) pure nothrow {
return n ? (n - male(female(n - 1))) : 1;
}
void main() {
iota(20).map!female().writeln();
iota(20).map!male().writeln();
}

View file

@ -0,0 +1,12 @@
int M(int n) => n==0?1:n-F(M(n-1));
int F(int n) => n==0?0:n-M(F(n-1));
main() {
String f="",m="";
for(int i=0;i<20;i++) {
m+="${M(i)} ";
f+="${F(i)} ";
}
print("M: $m");
print("F: $f");
}

View file

@ -0,0 +1,28 @@
unit Hofstadter;
interface
type
THofstadterFemaleMaleSequences = class
public
class function F(n: Integer): Integer;
class function M(n: Integer): Integer;
end;
implementation
class function THofstadterFemaleMaleSequences.F(n: Integer): Integer;
begin
Result:= 1;
if (n > 0) then
Result:= n - M(F(n-1));
end;
class function THofstadterFemaleMaleSequences.M(n: Integer): Integer;
begin
Result:= 0;
if (n > 0) then
Result:= n - F(M(n - 1));
end;
end.

View file

@ -0,0 +1,4 @@
def [F, M] := [
fn n { if (n <=> 0) { 1 } else { n - M(F(n - 1)) } },
fn n { if (n <=> 0) { 0 } else { n - F(M(n - 1)) } },
]

View file

@ -0,0 +1,3 @@
def M
def F(n) { return if (n <=> 0) { 1 } else { n - M(F(n - 1)) } }
bind M(n) { return if (n <=> 0) { 0 } else { n - F(M(n - 1)) } }

View file

@ -0,0 +1,13 @@
-module(mutrec).
-export([mutrec/0, f/1, m/1]).
f(0) -> 1;
f(N) -> N - m(f(N-1)).
m(0) -> 0;
m(N) -> N - f(m(N-1)).
mutrec() -> lists:map(fun(X) -> io:format("~w ", [f(X)]) end, lists:seq(0,19)),
io:format("~n", []),
lists:map(fun(X) -> io:format("~w ", [m(X)]) end, lists:seq(0,19)),
io:format("~n", []).

View file

@ -0,0 +1,21 @@
integer idM, idF
function F(integer n)
if n = 0 then
return 1
else
return n - call_func(idM,{F(n-1)})
end if
end function
idF = routine_id("F")
function M(integer n)
if n = 0 then
return 0
else
return n - call_func(idF,{M(n-1)})
end if
end function
idM = routine_id("M")

View file

@ -0,0 +1,5 @@
[$[$1-f;!m;!-1-]?1+]f:
[$[$1-m;!f;!- ]? ]m:
[0[$20\>][\$@$@!." "1+]#%%]t:
f; t;!"
"m; t;!

View file

@ -0,0 +1,3 @@
DEFER: F
: M ( n -- n' ) dup 0 = [ dup 1 - M F - ] unless ;
: F ( n -- n' ) dup 0 = [ drop 1 ] [ dup 1 - F M - ] if ;

View file

@ -0,0 +1,23 @@
class Main
{
static Int f (Int n)
{
if (n <= 0) // ensure n > 0
return 1
else
return n - m(f(n-1))
}
static Int m (Int n)
{
if (n <= 0) // ensure n > 0
return 0
else
return n - f(m(n-1))
}
public static Void main ()
{
50.times |Int n| { echo (f(n)) }
}
}

View file

@ -0,0 +1,15 @@
defer m
: f ( n -- n )
dup 0= if 1+ exit then
dup 1- recurse m - ;
:noname ( n -- n )
dup 0= if exit then
dup 1- recurse f - ;
is m
: test ( xt n -- ) cr 0 do i over execute . loop drop ;
' m defer@ 20 test \ 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12
' f 20 test \ 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12

View file

@ -0,0 +1,24 @@
module MutualRec
implicit none
contains
pure recursive function m(n) result(r)
integer :: r
integer, intent(in) :: n
if ( n == 0 ) then
r = 0
return
end if
r = n - f(m(n-1))
end function m
pure recursive function f(n) result(r)
integer :: r
integer, intent(in) :: n
if ( n == 0 ) then
r = 1
return
end if
r = n - m(f(n-1))
end function f
end module

View file

@ -0,0 +1,17 @@
program testmutrec
use MutualRec
implicit none
integer :: i
integer, dimension(20) :: a = (/ (i, i=0,19) /), b = (/ (i, i=0,19) /)
integer, dimension(20) :: ra, rb
forall(i=1:20)
ra(i) = m(a(i))
rb(i) = f(b(i))
end forall
write(*,'(20I3)') rb
write(*,'(20I3)') ra
end program testmutrec

View file

@ -0,0 +1,23 @@
package main
import "fmt"
func F(n int) int {
if n == 0 { return 1 }
return n - M(F(n-1))
}
func M(n int) int {
if n == 0 { return 0 }
return n - F(M(n-1))
}
func main() {
for i := 0; i < 20; i++ {
fmt.Printf("%2d ", F(i))
}
fmt.Println()
for i := 0; i < 20; i++ {
fmt.Printf("%2d ", M(i))
}
fmt.Println()
}

View file

@ -0,0 +1,3 @@
def f, m // recursive closures must be declared before they are defined
f = { n -> n == 0 ? 1 : n - m(f(n-1)) }
m = { n -> n == 0 ? 0 : n - f(m(n-1)) }

View file

@ -0,0 +1,2 @@
println 'f(0..20): ' + (0..20).collect { f(it) }
println 'm(0..20): ' + (0..20).collect { m(it) }

View file

@ -0,0 +1,9 @@
f 0 = 1
f n | n > 0 = n - m (f $ n-1)
m 0 = 0
m n | n > 0 = n - f (m $ n-1)
main = do
print $ map f [0..19]
print $ map m [0..19]

View file

@ -0,0 +1,13 @@
procedure main(arglist)
every write(F(!arglist)) # F of all arguments
end
procedure F(n)
if integer(n) >= 0 then
return (n = 0, 1) | n - M(F(n-1))
end
procedure M(n)
if integer(n) >= 0 then
return (0 = n) | n - F(M(n-1))
end

View file

@ -0,0 +1,2 @@
F =: 1:`(- M @ $: @ <:) @.* M."0
M =: 0:`(- F @ $: @ <:) @.* M."0

View file

@ -0,0 +1,2 @@
F i. 20
1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12

View file

@ -0,0 +1,18 @@
public static int f(final int n)
{
return n == 0 ? 1 : n - m(f(n - 1));
}
public static int m(final int n)
{
return n == 0 ? 0 : n - f(m(n - 1));
}
public static void main(final String args[])
{
for (int i = 0; i < 20; i++)
System.out.println(f(i));
System.out.println();
for (i = 0; i < 20; i++)
System.out.println(m(i));
}

View file

@ -0,0 +1,19 @@
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);

View file

@ -0,0 +1,30 @@
integer iDEPTH = 100;
integer f(integer n) {
if(n==0) {
return 1;
} else {
return n-m(f(n - 1));
}
}
integer m(integer n) {
if(n==0) {
return 0;
} else {
return n-f(m(n - 1));
}
}
default {
state_entry() {
integer x = 0;
string s = "";
for(x=0 ; x<iDEPTH ; x++) {
s += (string)(f(x))+" ";
}
llOwnerSay(llList2CSV(llParseString2List(s, [" "], [])));
s = "";
for(x=0 ; x<iDEPTH ; x++) {
s += (string)(m(x))+" ";
}
llOwnerSay(llList2CSV(llParseString2List(s, [" "], [])));
}
}

View file

@ -0,0 +1,27 @@
print "F sequence."
for i = 0 to 20
print f(i);" ";
next
print
print "M sequence."
for i = 0 to 20
print m(i);" ";
next
end
function f(n)
if n = 0 then
f = 1
else
f = n - m(f(n - 1))
end if
end function
function m(n)
if n = 0 then
m = 0
else
m = n - f(m(n - 1))
end if
end function

View file

@ -0,0 +1,13 @@
to m :n
if 0 = :n [output 0]
output :n - f m :n-1
end
to f :n
if 0 = :n [output 1]
output :n - m f :n-1
end
show cascade 20 [lput m #-1 ?] []
[1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12]
show cascade 20 [lput f #-1 ?] []
[0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12]

View file

@ -0,0 +1,2 @@
function m(n) return n > 0 and n - f(m(n-1)) or 0 end
function f(n) return n > 0 and n - m(f(n-1)) or 1 end

View file

@ -0,0 +1,3 @@
local m,n
function m(n) return n > 0 and n - f(m(n-1)) or 0 end
function f(n) return n > 0 and n - m(f(n-1)) or 1 end

View file

@ -0,0 +1,5 @@
define(`female',`ifelse(0,$1,1,`eval($1 - male(female(decr($1))))')')dnl
define(`male',`ifelse(0,$1,0,`eval($1 - female(male(decr($1))))')')dnl
define(`loop',`ifelse($1,$2,,`$3($1) loop(incr($1),$2,`$3')')')dnl
loop(0,20,`female')
loop(0,20,`male')

View file

@ -0,0 +1,9 @@
function Fn = female(n)
if n == 0
Fn = 1;
return
end
Fn = n - male(female(n-1));
end

View file

@ -0,0 +1,9 @@
function Mn = male(n)
if n == 0
Mn = 0;
return
end
Mn = n - female(male(n-1));
end

View file

@ -0,0 +1,12 @@
>> n = (0:10);
>> arrayfun(@female,n)
ans =
1 1 2 2 3 3 4 5 5 6 6
>> arrayfun(@male,n)
ans =
0 0 1 2 2 3 4 4 5 6 6

View file

@ -0,0 +1,78 @@
LOC Data_Segment
GREG @
NL BYTE #a,0
GREG @
buf OCTA 0,0
t IS $128
Ja IS $127
LOC #1000
GREG @
// print 2 digits integer with trailing space to StdOut
// reg $3 contains int to be printed
bp IS $71
0H GREG #0000000000203020
prtInt STO 0B,buf % initialize buffer
LDA bp,buf+7 % points after LSD
% REPEAT
1H SUB bp,bp,1 % move buffer pointer
DIV $3,$3,10 % divmod (x,10)
GET t,rR % get remainder
INCL t,'0' % make char digit
STB t,bp % store digit
PBNZ $3,1B % UNTIL no more digits
LDA $255,bp
TRAP 0,Fputs,StdOut % print integer
GO Ja,Ja,0 % 'return'
// Female function
F GET $1,rJ % save return addr
PBNZ $0,1F % if N != 0 then F N
INCL $0,1 % F 0 = 1
PUT rJ,$1 % restore return addr
POP 1,0 % return 1
1H SUBU $3,$0,1 % N1 = N - 1
PUSHJ $2,F % do F (N - 1)
ADDU $3,$2,0 % place result in arg. reg.
PUSHJ $2,M % do M F ( N - 1)
PUT rJ,$1 % restore ret addr
SUBU $0,$0,$2
POP 1,0 % return N - M F ( N - 1 )
// Male function
M GET $1,rJ
PBNZ $0,1F
PUT rJ,$1
POP 1,0 % return M 0 = 0
1H SUBU $3,$0,1
PUSHJ $2,M
ADDU $3,$2,0
PUSHJ $2,F
PUT rJ,$1
SUBU $0,$0,$2
POP 1,0 $ return N - F M ( N - 1 )
// do a female run
Main SET $1,0 % for (i=0; i<25; i++){
1H ADDU $4,$1,0 %
PUSHJ $3,F % F (i)
GO Ja,prtInt % print F (i)
INCL $1,1
CMP t,$1,25
PBNZ t,1B % }
LDA $255,NL
TRAP 0,Fputs,StdOut
// do a male run
SET $1,0 % for (i=0; i<25; i++){
1H ADDU $4,$1,0 %
PUSHJ $3,M % M (i)
GO Ja,prtInt % print M (i)
INCL $1,1
CMP t,$1,25
PBNZ t,1B % }
LDA $255,NL
TRAP 0,Fputs,StdOut
TRAP 0,Halt,0

View file

@ -0,0 +1,4 @@
f[0]:=1
m[0]:=0
f[n_]:=n-m[f[n-1]]
m[n_]:=n-f[m[n-1]]

View file

@ -0,0 +1,4 @@
f[0]:=1
m[0]:=0
f[n_]:=f[n]=n-m[f[n-1]]
m[n_]:=m[n]=n-f[m[n-1]]

View file

@ -0,0 +1,2 @@
m /@ Range[30]
f /@ Range[30]

View file

@ -0,0 +1,2 @@
{0,1,2,2,3,4,4,5,6,6,7,7,8,9,9,10,11,11,12,12,13,14,14,15,16,16,17,17,18,19}
{1,2,2,3,3,4,5,5,6,6,7,8,8,9,9,10,11,11,12,13,13,14,14,15,16,16,17,17,18,19}

View file

@ -0,0 +1,23 @@
f[0]: 1$
m[0]: 0$
f[n] := n - m[f[n - 1]]$
m[n] := n - f[m[n - 1]]$
makelist(f[i], i, 0, 10);
[1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6]
makelist(m[i], i, 0, 10);
[0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6]
remarray(m, f)$
f(n) := if n = 0 then 1 else n - m(f(n - 1))$
m(n) := if n = 0 then 0 else n - f(m(n - 1))$
makelist(f(i), i, 0, 10);
[1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6]
makelist(m(i), i, 0, 10);
[0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6]
remfunction(f, m)$

View file

@ -0,0 +1,20 @@
:- module mutual_recursion.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, list.
main(!IO) :-
io.write(list.map(f, 0..19), !IO), io.nl(!IO),
io.write(list.map(m, 0..19), !IO), io.nl(!IO).
:- func f(int) = int.
f(N) = ( if N = 0 then 1 else N - m(f(N - 1)) ).
:- func m(int) = int.
m(N) = ( if N = 0 then 0 else N - f(m(N - 1)) ).

View file

@ -0,0 +1,23 @@
<?php
function F($n)
{
if ( $n == 0 ) return 1;
return $n - M(F($n-1));
}
function M($n)
{
if ( $n == 0) return 0;
return $n - F(M($n-1));
}
$ra = array();
$rb = array();
for($i=0; $i < 20; $i++)
{
array_push($ra, F($i));
array_push($rb, M($i));
}
echo implode(" ", $ra) . "\n";
echo implode(" ", $rb) . "\n";
?>

View file

@ -0,0 +1,13 @@
use strict;
use warnings;
# For mutually recursive functions,
# predeclaring is probably a good idea.
sub M; sub F;
sub F { my $n = shift; $n ? $n - M F $n-1 : 1 }
sub M { my $n = shift; $n ? $n - F M $n-1 : 0 }
for my $f (\&F, \&M) {
print join(' ', map $f->($_), 0 .. 19), "\n";
}

View file

@ -0,0 +1,9 @@
(de f (N)
(if (=0 N)
1
(- N (m (f (dec N)))) ) )
(de m (N)
(if (=0 N)
0
(- N (f (m (dec N)))) ) )

View file

@ -0,0 +1,13 @@
female(0,1).
female(N,F) :- N>0,
N1 is N-1,
female(N1,R),
male(R, R1),
F is N-R1.
male(0,0).
male(N,F) :- N>0,
N1 is N-1,
male(N1,R),
female(R, R1),
F is N-R1.

View file

@ -0,0 +1,2 @@
flist(S) :- for(X, 0, S), female(X, R), format('~d ', [R]), fail.
mlist(S) :- for(X, 0, S), male(X, R), format('~d ', [R]), fail.

View file

@ -0,0 +1,5 @@
def F(n): return 1 if n == 0 else n - M(F(n-1))
def M(n): return 0 if n == 0 else n - F(M(n-1))
print ([ F(n) for n in range(20) ])
print ([ M(n) for n in range(20) ])

View file

@ -0,0 +1,2 @@
F <- function(n) ifelse(n == 0, 1, n - M(F(n-1)))
M <- function(n) ifelse(n == 0, 0, n - F(M(n-1)))

View file

@ -0,0 +1,2 @@
print.table(lapply(0:19, M))
print.table(lapply(0:19, F))

View file

@ -0,0 +1,11 @@
/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
arg lim .; if lim=='' then lim=40; 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; arg n; if n==0 then return 1; return n-M(F(n-1))
M: procedure; 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*/

View file

@ -0,0 +1,14 @@
/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
arg lim .;if lim=='' then lim=99; hm.=; hm.0=0; hf.=; hf.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.*/
/*─────────────────────────────────────F, M, Jw subroutines────────────*/
F: procedure expose hm. hf.; arg n; if hf.n=='' then hf.n=n-M(F(n-1)); return hf.n
M: procedure expose hm. hf.; 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*/

View file

@ -0,0 +1,18 @@
/*REXX program shows mutual recursion (via Hofstadter Male & Female seq)*/
/*If LIM is negative, only show a single result 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
do j=0 to Alim; ff=F(j); mm=M(j)
Js=Js jW(j); Fs=Fs jw(ff); Ms=Ms jW(mm)
end
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.*/
/*─────────────────────────────────────F, M, Jw subroutines────────────*/
F: procedure expose hm. hf.; arg n; if hf.n=='' then hf.n=n-M(F(n-1)); return hf.n
M: procedure expose hm. hf.; 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*/

View file

@ -0,0 +1,9 @@
def F(n)
n == 0 ? 1 : n - M(F(n-1))
end
def M(n)
n == 0 ? 0 : n - F(M(n-1))
end
p (Array.new(20) {|n| F(n) })
p (Array.new(20) {|n| M(n) })

View file

@ -0,0 +1,21 @@
/======\
F==!/=!\?\+# | />-<-\
| \@\-@/@\===?/<#
| | |
$+++/======|====/
! /=/ /+<<-\
| \!/======?\>>=?/<# dup
| \<<+>+>-/
! !
\======|====\
| | |
| /===|==\ |
M==!\=!\?\#| | |
\@/-@/@/===?\<#
^ \>-<-/
| ^ ^ ^ ^
| | | | subtract from n
| | | mutual recursion
| | recursion
| n-1
check for zero

View file

@ -0,0 +1,26 @@
class MAIN is
f(n:INT):INT
pre n >= 0
is
if n = 0 then return 1; end;
return n - m(f(n-1));
end;
m(n:INT):INT
pre n >= 0
is
if n = 0 then return 0; end;
return n - f(m(n-1));
end;
main is
loop i ::= 0.upto!(19);
#OUT + #FMT("%2d ", f(i));
end;
#OUT + "\n";
loop i ::= 0.upto!(19);
#OUT + #FMT("%2d ", m(i));
end;
end;
end;

View file

@ -0,0 +1,7 @@
def F(n:Int):Int =
if (n == 0) 1 else n - M(F(n-1))
def M(n:Int):Int =
if (n == 0) 0 else n - F(M(n-1))
println((0 until 20).map(F).mkString(", "))
println((0 until 20).map(M).mkString(", "))

View file

@ -0,0 +1,7 @@
(define (F n)
(if (= n 0) 1
(- n (M (F (- n 1))))))
(define (M n)
(if (= n 0) 0
(- n (F (M (- n 1))))))

View file

@ -0,0 +1,7 @@
(letrec ((F (lambda (n)
(if (= n 0) 1
(- n (M (F (- n 1)))))))
(M (lambda (n)
(if (= n 0) 0
(- n (F (M (- n 1))))))))
(F 19)) # evaluates to 12

View file

@ -0,0 +1,23 @@
|F M ra rb|
F := [ :n |
(n == 0)
ifTrue: [ 1 ]
ifFalse: [ n - (M value: (F value: (n-1))) ]
].
M := [ :n |
(n == 0)
ifTrue: [ 0 ]
ifFalse: [ n - (F value: (M value: (n-1))) ]
].
ra := OrderedCollection new.
rb := OrderedCollection new.
0 to: 19 do: [ :i |
ra add: (F value: i).
rb add: (M value: i)
].
ra displayNl.
rb displayNl.

View file

@ -0,0 +1,21 @@
proc m {n} {
if { $n == 0 } { expr 0; } else {
expr {$n - [f [m [expr {$n-1}] ]]};
}
}
proc f {n} {
if { $n == 0 } { expr 1; } else {
expr {$n - [m [f [expr {$n-1}] ]]};
}
}
for {set i 0} {$i < 20} {incr i} {
puts -nonewline [f $i];
puts -nonewline " ";
}
puts ""
for {set i 0} {$i < 20} {incr i} {
puts -nonewline [m $i];
puts -nonewline " ";
}
puts ""