Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Mutual-recursion/00-META.yaml
Normal file
3
Task/Mutual-recursion/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Mutual_recursion
|
||||
note: recursion
|
||||
18
Task/Mutual-recursion/00-TASK.txt
Normal file
18
Task/Mutual-recursion/00-TASK.txt
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
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:
|
||||
<big>
|
||||
:<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>
|
||||
</big>
|
||||
|
||||
<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).
|
||||
<br><br>
|
||||
|
||||
68
Task/Mutual-recursion/8080-Assembly/mutual-recursion.8080
Normal file
68
Task/Mutual-recursion/8080-Assembly/mutual-recursion.8080
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
org 100h
|
||||
jmp test
|
||||
;;; Implementation of F(A).
|
||||
F: ana a ; Zero?
|
||||
jz one ; Then set A=1
|
||||
mov b,a ; Otherwise, set B=A,
|
||||
push b ; And put it on the stack
|
||||
dcr a ; Set A=A-1
|
||||
call F ; Set A=F(A-1)
|
||||
call M ; Set A=M(F(A-1))
|
||||
pop b ; Retrieve input value
|
||||
cma ; (-A)+B is actually one cycle faster
|
||||
inr a ; than C=A;A=B;A-=B, and equivalent
|
||||
add b
|
||||
ret
|
||||
one: mvi a,1 ; Set A to 1,
|
||||
ret ; and return.
|
||||
;;; Implementation of M(A).
|
||||
M: ana a ; Zero?
|
||||
rz ; Then keep it that way and return.
|
||||
mov b,a
|
||||
push b ; Otherwise, same deal as in F,
|
||||
dcr a ; but M and F are called in opposite
|
||||
call M ; order.
|
||||
call F
|
||||
pop b
|
||||
cma
|
||||
inr a
|
||||
add b
|
||||
ret
|
||||
;;; Demonstration code.
|
||||
test: lhld 6 ; Set stack pointer to highest usable
|
||||
sphl ; memory.
|
||||
;;; Print F([0..15])
|
||||
lxi d,fpfx ; Print "F: "
|
||||
mvi c,9
|
||||
call 5
|
||||
xra a ; Start with N=0
|
||||
floop: push psw ; Keep N
|
||||
call F ; Get value for F(N)
|
||||
call pdgt ; Print it
|
||||
pop psw ; Restore N
|
||||
inr a ; Next N
|
||||
cpi 16 ; Done yet?
|
||||
jnz floop
|
||||
;;; Print M([0..15])
|
||||
lxi d,mpfx ; Print "\r\nM: "
|
||||
mvi c,9
|
||||
call 5
|
||||
xra a ; Start with N=0
|
||||
mloop: push psw ; same deal as above
|
||||
call M
|
||||
call pdgt
|
||||
pop psw ; Restore N
|
||||
inr a
|
||||
cpi 16
|
||||
jnz mloop
|
||||
rst 0 ; Explicit exit, we got rid of system stack
|
||||
;;; Print digit and space
|
||||
pdgt: adi '0' ; ASCII
|
||||
mov e,a
|
||||
mvi c,2
|
||||
call 5
|
||||
mvi e,' ' ; Space
|
||||
mvi c,2
|
||||
jmp 5 ; Tail call optimization
|
||||
fpfx: db 'F: $'
|
||||
mpfx: db 13,10,'M: $'
|
||||
47
Task/Mutual-recursion/ABAP/mutual-recursion.abap
Normal file
47
Task/Mutual-recursion/ABAP/mutual-recursion.abap
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
report z_mutual_recursion.
|
||||
|
||||
class hoffstadter_sequences definition.
|
||||
public section.
|
||||
class-methods:
|
||||
f
|
||||
importing
|
||||
n type int4
|
||||
returning
|
||||
value(result) type int4,
|
||||
|
||||
m
|
||||
importing
|
||||
n type int4
|
||||
returning
|
||||
value(result) type int4.
|
||||
endclass.
|
||||
|
||||
|
||||
class hoffstadter_sequences implementation.
|
||||
method f.
|
||||
result = cond int4(
|
||||
when n eq 0
|
||||
then 1
|
||||
else n - m( f( n - 1 ) ) ).
|
||||
endmethod.
|
||||
|
||||
|
||||
method m.
|
||||
result = cond int4(
|
||||
when n eq 0
|
||||
then 0
|
||||
else n - f( m( n - 1 ) ) ).
|
||||
endmethod.
|
||||
endclass.
|
||||
|
||||
|
||||
start-of-selection.
|
||||
write: |{ reduce string(
|
||||
init results = |f(0 - 19): { hoffstadter_sequences=>f( 0 ) }|
|
||||
for i = 1 while i < 20
|
||||
next results = |{ results }, { hoffstadter_sequences=>f( i ) }| ) }|, /.
|
||||
|
||||
write: |{ reduce string(
|
||||
init results = |m(0 - 19): { hoffstadter_sequences=>m( 0 ) }|
|
||||
for i = 1 while i < 20
|
||||
next results = |{ results }, { hoffstadter_sequences=>m( i ) }| ) }|, /.
|
||||
12
Task/Mutual-recursion/ACL2/mutual-recursion.acl2
Normal file
12
Task/Mutual-recursion/ACL2/mutual-recursion.acl2
Normal 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)))))))
|
||||
21
Task/Mutual-recursion/ALGOL-68/mutual-recursion.alg
Normal file
21
Task/Mutual-recursion/ALGOL-68/mutual-recursion.alg
Normal 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)
|
||||
)
|
||||
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.
|
||||
3
Task/Mutual-recursion/APL/mutual-recursion.apl
Normal file
3
Task/Mutual-recursion/APL/mutual-recursion.apl
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
f ← {⍵=0:1 ⋄ ⍵-m∇⍵-1}
|
||||
m ← {⍵=0:0 ⋄ ⍵-f∇⍵-1}
|
||||
⍉'nFM'⍪↑(⊢,f,m)¨0,⍳20
|
||||
66
Task/Mutual-recursion/ARM-Assembly/mutual-recursion.arm
Normal file
66
Task/Mutual-recursion/ARM-Assembly/mutual-recursion.arm
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
.text
|
||||
.global _start
|
||||
@@@ Implementation of F(n), n in R0. n is considered unsigned.
|
||||
F: tst r0,r0 @ n = 0?
|
||||
moveq r0,#1 @ In that case, the result is 1
|
||||
bxeq lr @ And we can return to the caller
|
||||
push {r0,lr} @ Save link register and argument to stack
|
||||
sub r0,r0,#1 @ r0 -= 1 = n-1
|
||||
bl F @ r0 = F(r0) = F(n-1)
|
||||
bl M @ r0 = M(r0) = M(F(n-1))
|
||||
pop {r1,lr} @ Restore link register and argument in r1
|
||||
sub r0,r1,r0 @ Result is n-F(M(n-1))
|
||||
bx lr @ Return to caller.
|
||||
|
||||
@@@ Implementation of M(n), n in R0. n is considered unsigned.
|
||||
M: tst r0,r0 @ n = 0?
|
||||
bxeq lr @ In that case the result is also 0; return.
|
||||
push {r0,lr} @ Save link register and argument to stack
|
||||
sub r0,r0,#1 @ r0 -= 1 = n-1
|
||||
bl M @ r0 = M(r0) = M(n-1)
|
||||
bl F @ r0 = M(r0) = F(M(n-1))
|
||||
pop {r1,lr} @ Restore link register and argument in r1
|
||||
sub r0,r1,r0 @ Result is n-M(F(n-1))
|
||||
bx lr @ Return to caller
|
||||
|
||||
@@@ Print F(0..15) and M(0..15)
|
||||
_start: ldr r1,=fmsg @ Print values for F
|
||||
ldr r4,=F
|
||||
bl prfn
|
||||
ldr r1,=mmsg @ Print values for M
|
||||
ldr r4,=M
|
||||
bl prfn
|
||||
mov r7,#1 @ Exit process
|
||||
swi #0
|
||||
|
||||
@@@ Helper function for output: print [r1], then [r4](0..15)
|
||||
@@@ This assumes [r4] preserves r3 and r4; M and F do.
|
||||
prfn: push {lr} @ Keep link register
|
||||
bl pstr @ Print the string
|
||||
mov r3,#0 @ Start at 0
|
||||
1: mov r0,r3 @ Call the function in r4 with current number
|
||||
blx r4
|
||||
add r0,r0,#'0 @ Make ASCII digit
|
||||
ldr r1,=dgt @ Store in digit string
|
||||
strb r0,[r1]
|
||||
ldr r1,=dstr @ Print result
|
||||
bl pstr
|
||||
add r3,r3,#1 @ Next number
|
||||
cmp r3,#15 @ Keep going up to and including 15
|
||||
bls 1b
|
||||
ldr r1,=nl @ Print newline afterwards
|
||||
bl pstr
|
||||
pop {pc} @ Return to address on stack
|
||||
@@@ Print length-prefixed string r1 to stdout
|
||||
pstr: push {lr} @ Keep link register
|
||||
mov r0,#1 @ stdout = 1
|
||||
ldrb r2,[r1],#1 @ r2 = length prefix
|
||||
mov r7,#4 @ 4 = write syscall
|
||||
swi #0
|
||||
pop {pc} @ Return to address on stack
|
||||
.data
|
||||
fmsg: .ascii "\3F: "
|
||||
mmsg: .ascii "\3M: "
|
||||
dstr: .ascii "\2"
|
||||
dgt: .ascii "* "
|
||||
nl: .ascii "\1\n"
|
||||
20
Task/Mutual-recursion/AWK/mutual-recursion.awk
Normal file
20
Task/Mutual-recursion/AWK/mutual-recursion.awk
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
cat mutual_recursion.awk:
|
||||
#!/usr/local/bin/gawk -f
|
||||
|
||||
# User defined functions
|
||||
function F(n)
|
||||
{ return n == 0 ? 1 : n - M(F(n-1)) }
|
||||
|
||||
function M(n)
|
||||
{ return n == 0 ? 0 : 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 ""
|
||||
}
|
||||
28
Task/Mutual-recursion/Ada/mutual-recursion-1.ada
Normal file
28
Task/Mutual-recursion/Ada/mutual-recursion-1.ada
Normal 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;
|
||||
20
Task/Mutual-recursion/Ada/mutual-recursion-2.ada
Normal file
20
Task/Mutual-recursion/Ada/mutual-recursion-2.ada
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
procedure Mutual_Recursion is
|
||||
function M(N: Natural) return Natural;
|
||||
function F(N: Natural) return Natural;
|
||||
|
||||
function M(N: Natural) return Natural is
|
||||
(if N = 0 then 0 else N – F(M(N–1)));
|
||||
|
||||
function F(N: Natural) return Natural is
|
||||
(if N =0 then 1 else N – M(F(N–1)));
|
||||
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;
|
||||
42
Task/Mutual-recursion/Aime/mutual-recursion.aime
Normal file
42
Task/Mutual-recursion/Aime/mutual-recursion.aime
Normal 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;
|
||||
}
|
||||
|
|
@ -0,0 +1,66 @@
|
|||
-- f :: Int -> Int
|
||||
on f(x)
|
||||
if x = 0 then
|
||||
1
|
||||
else
|
||||
x - m(f(x - 1))
|
||||
end if
|
||||
end f
|
||||
|
||||
-- m :: Int -> Int
|
||||
on m(x)
|
||||
if x = 0 then
|
||||
0
|
||||
else
|
||||
x - f(m(x - 1))
|
||||
end if
|
||||
end m
|
||||
|
||||
|
||||
-- TEST
|
||||
on run
|
||||
set xs to range(0, 19)
|
||||
|
||||
{map(f, xs), map(m, xs)}
|
||||
end run
|
||||
|
||||
|
||||
-- GENERIC FUNCTIONS
|
||||
|
||||
-- map :: (a -> b) -> [a] -> [b]
|
||||
on map(f, xs)
|
||||
tell mReturn(f)
|
||||
set lng to length of xs
|
||||
set lst to {}
|
||||
repeat with i from 1 to lng
|
||||
set end of lst to lambda(item i of xs, i, xs)
|
||||
end repeat
|
||||
return lst
|
||||
end tell
|
||||
end map
|
||||
|
||||
-- Lift 2nd class handler function into 1st class script wrapper
|
||||
-- mReturn :: Handler -> Script
|
||||
on mReturn(f)
|
||||
if class of f is script then
|
||||
f
|
||||
else
|
||||
script
|
||||
property lambda : f
|
||||
end script
|
||||
end if
|
||||
end mReturn
|
||||
|
||||
-- range :: Int -> Int -> [Int]
|
||||
on range(m, n)
|
||||
if n < m then
|
||||
set d to -1
|
||||
else
|
||||
set d to 1
|
||||
end if
|
||||
set lst to {}
|
||||
repeat with i from m to n by d
|
||||
set end of lst to i
|
||||
end repeat
|
||||
return lst
|
||||
end range
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
{{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}}
|
||||
8
Task/Mutual-recursion/Arturo/mutual-recursion.arturo
Normal file
8
Task/Mutual-recursion/Arturo/mutual-recursion.arturo
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
f: $[n][ if? n=0 -> 1 else -> n-m f n-1 ]
|
||||
m: $[n][ if? n=0 -> 0 else -> n-f m n-1 ]
|
||||
|
||||
loop 0..20 'i [
|
||||
print ["f(" i ")=" f i]
|
||||
print ["m(" i ")=" m i]
|
||||
print ""
|
||||
]
|
||||
11
Task/Mutual-recursion/AutoHotkey/mutual-recursion-1.ahk
Normal file
11
Task/Mutual-recursion/AutoHotkey/mutual-recursion-1.ahk
Normal 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
|
||||
}
|
||||
31
Task/Mutual-recursion/AutoHotkey/mutual-recursion-2.ahk
Normal file
31
Task/Mutual-recursion/AutoHotkey/mutual-recursion-2.ahk
Normal 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
|
||||
}
|
||||
18
Task/Mutual-recursion/BASIC/mutual-recursion.basic
Normal file
18
Task/Mutual-recursion/BASIC/mutual-recursion.basic
Normal 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
|
||||
20
Task/Mutual-recursion/BASIC256/mutual-recursion.basic
Normal file
20
Task/Mutual-recursion/BASIC256/mutual-recursion.basic
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
# Rosetta Code problem: http://rosettacode.org/wiki/Mutual_recursion
|
||||
# by Jjuanhdez, 06/2022
|
||||
|
||||
n = 24
|
||||
print "n : ";
|
||||
for i = 0 to n : print ljust(i, 3); : next i
|
||||
print chr(10); ("-" * 78)
|
||||
print "F : ";
|
||||
for i = 0 to n : print ljust(F(i), 3); : next i
|
||||
print chr(10); "M : ";
|
||||
for i = 0 to n : print ljust(M(i), 3); : next i
|
||||
end
|
||||
|
||||
function F(n)
|
||||
if n = 0 then return 0 else return n - M(F(n-1))
|
||||
end function
|
||||
|
||||
function M(n)
|
||||
if n = 0 then return 0 else return n - F(M(n-1))
|
||||
end function
|
||||
16
Task/Mutual-recursion/BBC-BASIC/mutual-recursion.basic
Normal file
16
Task/Mutual-recursion/BBC-BASIC/mutual-recursion.basic
Normal 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))
|
||||
20
Task/Mutual-recursion/BCPL/mutual-recursion.bcpl
Normal file
20
Task/Mutual-recursion/BCPL/mutual-recursion.bcpl
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
get "libhdr"
|
||||
|
||||
// Mutually recursive functions
|
||||
let f(n) = n=0 -> 1, n - m(f(n-1))
|
||||
and m(n) = n=0 -> 0, n - f(m(n-1))
|
||||
|
||||
// Print f(0..15) and m(0..15)
|
||||
let start() be
|
||||
$( writes("F:")
|
||||
for i=0 to 15 do
|
||||
$( writes(" ")
|
||||
writen(f(i))
|
||||
$)
|
||||
writes("*NM:")
|
||||
for i=0 to 15 do
|
||||
$( writes(" ")
|
||||
writen(m(i))
|
||||
$)
|
||||
writes("*N")
|
||||
$)
|
||||
3
Task/Mutual-recursion/BQN/mutual-recursion.bqn
Normal file
3
Task/Mutual-recursion/BQN/mutual-recursion.bqn
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
F ← {0:1; 𝕩-M F𝕩-1}
|
||||
M ← {0:0; 𝕩-F M𝕩-1}
|
||||
⍉"FM"∾>(F∾M)¨↕15
|
||||
21
Task/Mutual-recursion/BaCon/mutual-recursion.bacon
Normal file
21
Task/Mutual-recursion/BaCon/mutual-recursion.bacon
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
' Mutually recursive
|
||||
FUNCTION F(int n) TYPE int
|
||||
RETURN IIF(n = 0, 1, n - M(F(n -1)))
|
||||
END FUNCTION
|
||||
|
||||
FUNCTION M(int n) TYPE int
|
||||
RETURN IIF(n = 0, 0, n - F(M(n - 1)))
|
||||
END FUNCTION
|
||||
|
||||
' Get iteration limit, default 20
|
||||
SPLIT ARGUMENT$ BY " " TO arg$ SIZE args
|
||||
limit = IIF(args > 1, VAL(arg$[1]), 20)
|
||||
|
||||
FOR i = 0 TO limit
|
||||
PRINT F(i) FORMAT "%2d "
|
||||
NEXT
|
||||
PRINT
|
||||
FOR i = 0 TO limit
|
||||
PRINT M(i) FORMAT "%2d "
|
||||
NEXT
|
||||
PRINT
|
||||
10
Task/Mutual-recursion/Bc/mutual-recursion-1.bc
Normal file
10
Task/Mutual-recursion/Bc/mutual-recursion-1.bc
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
cat mutual_recursion.bc:
|
||||
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)));
|
||||
}
|
||||
10
Task/Mutual-recursion/Bc/mutual-recursion-2.bc
Normal file
10
Task/Mutual-recursion/Bc/mutual-recursion-2.bc
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* 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";
|
||||
quit
|
||||
8
Task/Mutual-recursion/Bracmat/mutual-recursion.bracmat
Normal file
8
Task/Mutual-recursion/Bracmat/mutual-recursion.bracmat
Normal 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
|
||||
16
Task/Mutual-recursion/Brat/mutual-recursion.brat
Normal file
16
Task/Mutual-recursion/Brat/mutual-recursion.brat
Normal 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 }
|
||||
36
Task/Mutual-recursion/C++/mutual-recursion-1.cpp
Normal file
36
Task/Mutual-recursion/C++/mutual-recursion-1.cpp
Normal 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;
|
||||
}
|
||||
18
Task/Mutual-recursion/C++/mutual-recursion-2.cpp
Normal file
18
Task/Mutual-recursion/C++/mutual-recursion-2.cpp
Normal 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));
|
||||
}
|
||||
21
Task/Mutual-recursion/C-sharp/mutual-recursion.cs
Normal file
21
Task/Mutual-recursion/C-sharp/mutual-recursion.cs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
namespace RosettaCode {
|
||||
class Hofstadter {
|
||||
static public int F(int n) {
|
||||
int result = 1;
|
||||
if (n > 0) {
|
||||
result = n - M(F(n-1));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static public int M(int n) {
|
||||
int result = 0;
|
||||
if (n > 0) {
|
||||
result = n - F(M(n - 1));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
30
Task/Mutual-recursion/C/mutual-recursion.c
Normal file
30
Task/Mutual-recursion/C/mutual-recursion.c
Normal 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;
|
||||
}
|
||||
36
Task/Mutual-recursion/CLU/mutual-recursion.clu
Normal file
36
Task/Mutual-recursion/CLU/mutual-recursion.clu
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
% To declare things you can either write an .spc file or you can use
|
||||
% the clu file itself as a specfile. For a small program a common
|
||||
% idiom is to spec and compile the same source file:
|
||||
%
|
||||
% pclu -spec mutrec.clu -clu mutrec.clu
|
||||
%
|
||||
start_up = proc ()
|
||||
print_first_16("F", F)
|
||||
print_first_16("M", M)
|
||||
end start_up
|
||||
|
||||
% Print the first few values for F and M
|
||||
print_first_16 = proc (name: string, fn: proctype (int) returns (int))
|
||||
po: stream := stream$primary_output()
|
||||
stream$puts(po, name || ":")
|
||||
for i: int in int$from_to(0, 15) do
|
||||
stream$puts(po, " " || int$unparse(fn(i)))
|
||||
end
|
||||
stream$putl(po, "")
|
||||
end print_first_16
|
||||
|
||||
F = proc (n: int) returns (int)
|
||||
if n = 0 then
|
||||
return (1)
|
||||
else
|
||||
return (n - M(F(n-1)))
|
||||
end
|
||||
end F
|
||||
|
||||
M = proc (n: int) returns (int)
|
||||
if n = 0 then
|
||||
return (0)
|
||||
else
|
||||
return (n - F(M(n-1)))
|
||||
end
|
||||
end M
|
||||
14
Task/Mutual-recursion/Ceylon/mutual-recursion.ceylon
Normal file
14
Task/Mutual-recursion/Ceylon/mutual-recursion.ceylon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
Integer f(Integer n)
|
||||
=> if (n > 0)
|
||||
then n - m(f(n-1))
|
||||
else 1;
|
||||
|
||||
Integer m(Integer n)
|
||||
=> if (n > 0)
|
||||
then n - f(m(n-1))
|
||||
else 0;
|
||||
|
||||
shared void run() {
|
||||
printAll((0:20).map(f));
|
||||
printAll((0:20).map(m));
|
||||
}
|
||||
11
Task/Mutual-recursion/Clojure/mutual-recursion.clj
Normal file
11
Task/Mutual-recursion/Clojure/mutual-recursion.clj
Normal 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))))))
|
||||
|
|
@ -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
|
||||
|
|
@ -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 ]
|
||||
9
Task/Mutual-recursion/Common-Lisp/mutual-recursion.lisp
Normal file
9
Task/Mutual-recursion/Common-Lisp/mutual-recursion.lisp
Normal 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))))))
|
||||
14
Task/Mutual-recursion/D/mutual-recursion.d
Normal file
14
Task/Mutual-recursion/D/mutual-recursion.d
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
import std.stdio, std.algorithm, std.range;
|
||||
|
||||
int male(in int n) pure nothrow {
|
||||
return n ? n - male(n - 1).female : 0;
|
||||
}
|
||||
|
||||
int female(in int n) pure nothrow {
|
||||
return n ? n - female(n - 1).male : 1;
|
||||
}
|
||||
|
||||
void main() {
|
||||
20.iota.map!female.writeln;
|
||||
20.iota.map!male.writeln;
|
||||
}
|
||||
12
Task/Mutual-recursion/Dart/mutual-recursion.dart
Normal file
12
Task/Mutual-recursion/Dart/mutual-recursion.dart
Normal 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");
|
||||
}
|
||||
28
Task/Mutual-recursion/Delphi/mutual-recursion.delphi
Normal file
28
Task/Mutual-recursion/Delphi/mutual-recursion.delphi
Normal 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.
|
||||
26
Task/Mutual-recursion/Draco/mutual-recursion.draco
Normal file
26
Task/Mutual-recursion/Draco/mutual-recursion.draco
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/* We need to predeclare M if we want F to be able to see it.
|
||||
* This is done using 'extern', same as if it had been in a
|
||||
* different compilation unit. */
|
||||
extern M(byte n) byte;
|
||||
|
||||
/* Mutually recursive functions */
|
||||
proc F(byte n) byte:
|
||||
if n=0 then 1 else n - M(F(n-1)) fi
|
||||
corp
|
||||
|
||||
proc M(byte n) byte:
|
||||
if n=0 then 0 else n - F(M(n-1)) fi
|
||||
corp
|
||||
|
||||
/* Show the first 16 values of each */
|
||||
proc nonrec main() void:
|
||||
byte i;
|
||||
|
||||
write("F:");
|
||||
for i from 0 upto 15 do write(F(i):2) od;
|
||||
writeln();
|
||||
|
||||
write("M:");
|
||||
for i from 0 upto 15 do write(M(i):2) od;
|
||||
writeln()
|
||||
corp
|
||||
9
Task/Mutual-recursion/Dyalect/mutual-recursion.dyalect
Normal file
9
Task/Mutual-recursion/Dyalect/mutual-recursion.dyalect
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
func f(n) {
|
||||
n == 0 ? 1 : n - m(f(n-1))
|
||||
}
|
||||
and m(n) {
|
||||
n == 0 ? 0 : n - f(m(n-1))
|
||||
}
|
||||
|
||||
print( (0..20).Map(i => f(i)).ToArray() )
|
||||
print( (0..20).Map(i => m(i)).ToArray() )
|
||||
4
Task/Mutual-recursion/E/mutual-recursion-1.e
Normal file
4
Task/Mutual-recursion/E/mutual-recursion-1.e
Normal 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)) } },
|
||||
]
|
||||
3
Task/Mutual-recursion/E/mutual-recursion-2.e
Normal file
3
Task/Mutual-recursion/E/mutual-recursion-2.e
Normal 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)) } }
|
||||
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
|
||||
20
Task/Mutual-recursion/Elena/mutual-recursion.elena
Normal file
20
Task/Mutual-recursion/Elena/mutual-recursion.elena
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
import extensions;
|
||||
import system'collections;
|
||||
|
||||
F = (n => (n == 0) ? 1 : (n - M(F(n-1))) );
|
||||
M = (n => (n == 0) ? 0 : (n - F(M(n-1))) );
|
||||
|
||||
public program()
|
||||
{
|
||||
var ra := new ArrayList();
|
||||
var rb := new ArrayList();
|
||||
|
||||
for(int i := 0, i <= 19, i += 1)
|
||||
{
|
||||
ra.append(F(i));
|
||||
rb.append(M(i))
|
||||
};
|
||||
|
||||
console.printLine(ra.asEnumerable());
|
||||
console.printLine(rb.asEnumerable())
|
||||
}
|
||||
9
Task/Mutual-recursion/Elixir/mutual-recursion.elixir
Normal file
9
Task/Mutual-recursion/Elixir/mutual-recursion.elixir
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
defmodule MutualRecursion do
|
||||
def f(0), do: 1
|
||||
def f(n), do: n - m(f(n - 1))
|
||||
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)
|
||||
13
Task/Mutual-recursion/Erlang/mutual-recursion.erl
Normal file
13
Task/Mutual-recursion/Erlang/mutual-recursion.erl
Normal 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", []).
|
||||
21
Task/Mutual-recursion/Euphoria/mutual-recursion.euphoria
Normal file
21
Task/Mutual-recursion/Euphoria/mutual-recursion.euphoria
Normal 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")
|
||||
8
Task/Mutual-recursion/F-Sharp/mutual-recursion.fs
Normal file
8
Task/Mutual-recursion/F-Sharp/mutual-recursion.fs
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
let rec f n =
|
||||
match n with
|
||||
| 0 -> 1
|
||||
| _ -> n - (m (f (n-1)))
|
||||
and m n =
|
||||
match n with
|
||||
| 0 -> 0
|
||||
| _ -> n - (f (m (n-1)))
|
||||
5
Task/Mutual-recursion/FALSE/mutual-recursion.false
Normal file
5
Task/Mutual-recursion/FALSE/mutual-recursion.false
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
[$[$1-f;!m;!-1-]?1+]f:
|
||||
[$[$1-m;!f;!- ]? ]m:
|
||||
[0[$20\>][\$@$@!." "1+]#%%]t:
|
||||
f; t;!"
|
||||
"m; t;!
|
||||
19
Task/Mutual-recursion/FOCAL/mutual-recursion.focal
Normal file
19
Task/Mutual-recursion/FOCAL/mutual-recursion.focal
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
01.01 C--PRINT F(0..15) AND M(0..15)
|
||||
01.10 T "F(0..15)"
|
||||
01.20 F X=0,15;S N=X;D 4;T %1,N
|
||||
01.30 T !"M(0..15)"
|
||||
01.40 F X=0,15;S N=X;D 5;T %1,N
|
||||
01.50 T !
|
||||
01.60 Q
|
||||
|
||||
04.01 C--N = F(N)
|
||||
04.10 I (N(D)),4.11,4.2
|
||||
04.11 S N(D)=1;R
|
||||
04.20 S D=D+1;S N(D)=N(D-1)-1;D 4;D 5
|
||||
04.30 S D=D-1;S N(D)=N(D)-N(D+1)
|
||||
|
||||
05.01 C--N = M(N)
|
||||
05.10 I (N(D)),5.11,5.2
|
||||
05.11 R
|
||||
05.20 S D=D+1;S N(D)=N(D-1)-1;D 5;D 4
|
||||
05.30 S D=D-1;S N(D)=N(D)-N(D+1)
|
||||
3
Task/Mutual-recursion/Factor/mutual-recursion.factor
Normal file
3
Task/Mutual-recursion/Factor/mutual-recursion.factor
Normal 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 ;
|
||||
23
Task/Mutual-recursion/Fantom/mutual-recursion.fantom
Normal file
23
Task/Mutual-recursion/Fantom/mutual-recursion.fantom
Normal 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)) }
|
||||
}
|
||||
}
|
||||
15
Task/Mutual-recursion/Forth/mutual-recursion.fth
Normal file
15
Task/Mutual-recursion/Forth/mutual-recursion.fth
Normal 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
|
||||
24
Task/Mutual-recursion/Fortran/mutual-recursion-1.f
Normal file
24
Task/Mutual-recursion/Fortran/mutual-recursion-1.f
Normal 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
|
||||
17
Task/Mutual-recursion/Fortran/mutual-recursion-2.f
Normal file
17
Task/Mutual-recursion/Fortran/mutual-recursion-2.f
Normal 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
|
||||
33
Task/Mutual-recursion/FreeBASIC/mutual-recursion.basic
Normal file
33
Task/Mutual-recursion/FreeBASIC/mutual-recursion.basic
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
' Need forward declaration of M as it's used
|
||||
' by F before its defined
|
||||
Declare Function M(n As Integer) As Integer
|
||||
|
||||
Function F(n As Integer) As Integer
|
||||
If n = 0 Then
|
||||
Return 1
|
||||
End If
|
||||
Return n - M(F(n - 1))
|
||||
End Function
|
||||
|
||||
Function M(n As Integer) As Integer
|
||||
If n = 0 Then
|
||||
Return 0
|
||||
End If
|
||||
Return n - F(M(n - 1))
|
||||
End Function
|
||||
|
||||
Dim As Integer n = 24
|
||||
Print "n :";
|
||||
For i As Integer = 0 to n : Print Using "###"; i; : Next
|
||||
Print
|
||||
Print String(78, "-")
|
||||
Print "F :";
|
||||
For i As Integer = 0 To n : Print Using "###"; F(i); : Next
|
||||
Print
|
||||
Print "M :";
|
||||
For i As Integer = 0 To n : Print Using "###"; M(i); : Next
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
23
Task/Mutual-recursion/Go/mutual-recursion.go
Normal file
23
Task/Mutual-recursion/Go/mutual-recursion.go
Normal 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()
|
||||
}
|
||||
3
Task/Mutual-recursion/Groovy/mutual-recursion-1.groovy
Normal file
3
Task/Mutual-recursion/Groovy/mutual-recursion-1.groovy
Normal 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)) }
|
||||
2
Task/Mutual-recursion/Groovy/mutual-recursion-2.groovy
Normal file
2
Task/Mutual-recursion/Groovy/mutual-recursion-2.groovy
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
println 'f(0..20): ' + (0..20).collect { f(it) }
|
||||
println 'm(0..20): ' + (0..20).collect { m(it) }
|
||||
9
Task/Mutual-recursion/Haskell/mutual-recursion.hs
Normal file
9
Task/Mutual-recursion/Haskell/mutual-recursion.hs
Normal 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]
|
||||
23
Task/Mutual-recursion/IS-BASIC/mutual-recursion.basic
Normal file
23
Task/Mutual-recursion/IS-BASIC/mutual-recursion.basic
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
100 PROGRAM "Hofstad.bas"
|
||||
110 PRINT "F sequence:"
|
||||
120 FOR I=0 TO 20
|
||||
130 PRINT F(I);
|
||||
140 NEXT
|
||||
150 PRINT :PRINT "M sequence:"
|
||||
160 FOR I=0 TO 20
|
||||
170 PRINT M(I);
|
||||
180 NEXT
|
||||
190 DEF F(N)
|
||||
200 IF N=0 THEN
|
||||
210 LET F=1
|
||||
220 ELSE
|
||||
230 LET F=N-M(F(N-1))
|
||||
240 END IF
|
||||
250 END DEF
|
||||
260 DEF M(N)
|
||||
270 IF N=0 THEN
|
||||
280 LET M=0
|
||||
290 ELSE
|
||||
300 LET M=N-F(M(N-1))
|
||||
310 END IF
|
||||
320 END DEF
|
||||
13
Task/Mutual-recursion/Icon/mutual-recursion.icon
Normal file
13
Task/Mutual-recursion/Icon/mutual-recursion.icon
Normal 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
|
||||
9
Task/Mutual-recursion/Idris/mutual-recursion.idris
Normal file
9
Task/Mutual-recursion/Idris/mutual-recursion.idris
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
mutual {
|
||||
F : Nat -> Nat
|
||||
F Z = (S Z)
|
||||
F (S n) = (S n) `minus` M(F(n))
|
||||
|
||||
M : Nat -> Nat
|
||||
M Z = Z
|
||||
M (S n) = (S n) `minus` F(M(n))
|
||||
}
|
||||
6
Task/Mutual-recursion/Io/mutual-recursion.io
Normal file
6
Task/Mutual-recursion/Io/mutual-recursion.io
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
f := method(n, if( n == 0, 1, n - m(f(n-1))))
|
||||
m := method(n, if( n == 0, 0, n - f(m(n-1))))
|
||||
|
||||
Range
|
||||
0 to(19) map(n,f(n)) println
|
||||
0 to(19) map(n,m(n)) println
|
||||
2
Task/Mutual-recursion/J/mutual-recursion-1.j
Normal file
2
Task/Mutual-recursion/J/mutual-recursion-1.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
F =: 1:`(- M @ $: @ <:) @.* M."0
|
||||
M =: 0:`(- F @ $: @ <:) @.* M."0
|
||||
2
Task/Mutual-recursion/J/mutual-recursion-2.j
Normal file
2
Task/Mutual-recursion/J/mutual-recursion-2.j
Normal 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
|
||||
41
Task/Mutual-recursion/Java/mutual-recursion.java
Normal file
41
Task/Mutual-recursion/Java/mutual-recursion.java
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class MutualRecursion {
|
||||
|
||||
public static void main(final String args[]) {
|
||||
int max = 20;
|
||||
System.out.printf("First %d values of the Female sequence: %n", max);
|
||||
for (int i = 0; i < max; i++) {
|
||||
System.out.printf(" f(%d) = %d%n", i, f(i));
|
||||
}
|
||||
System.out.printf("First %d values of the Male sequence: %n", max);
|
||||
for (int i = 0; i < 20; i++) {
|
||||
System.out.printf(" m(%d) = %d%n", i, m(i));
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<Integer,Integer> F_MAP = new HashMap<>();
|
||||
|
||||
private static int f(final int n) {
|
||||
if ( F_MAP.containsKey(n) ) {
|
||||
return F_MAP.get(n);
|
||||
}
|
||||
int fn = n == 0 ? 1 : n - m(f(n - 1));
|
||||
F_MAP.put(n, fn);
|
||||
return fn;
|
||||
}
|
||||
|
||||
private static Map<Integer,Integer> M_MAP = new HashMap<>();
|
||||
|
||||
private static int m(final int n) {
|
||||
if ( M_MAP.containsKey(n) ) {
|
||||
return M_MAP.get(n);
|
||||
}
|
||||
int mn = n == 0 ? 0 : n - f(m(n - 1));
|
||||
M_MAP.put(n, mn);
|
||||
return mn;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
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
Task/Mutual-recursion/JavaScript/mutual-recursion-3.js
Normal file
1
Task/Mutual-recursion/JavaScript/mutual-recursion-3.js
Normal file
|
|
@ -0,0 +1 @@
|
|||
var range = (m, n) => Array(... Array(n - m + 1)).map((x, i) => m + i)
|
||||
6
Task/Mutual-recursion/Jq/mutual-recursion-1.jq
Normal file
6
Task/Mutual-recursion/Jq/mutual-recursion-1.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def M:
|
||||
def F: if . == 0 then 1 else . - ((. - 1) | F | M) end;
|
||||
if . == 0 then 0 else . - ((. - 1) | M | F) end;
|
||||
|
||||
def F:
|
||||
if . == 0 then 1 else . - ((. - 1) | F | M) end;
|
||||
2
Task/Mutual-recursion/Jq/mutual-recursion-2.jq
Normal file
2
Task/Mutual-recursion/Jq/mutual-recursion-2.jq
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
[range(0;20) | F],
|
||||
[range(0;20) | M]
|
||||
4
Task/Mutual-recursion/Jq/mutual-recursion-3.jq
Normal file
4
Task/Mutual-recursion/Jq/mutual-recursion-3.jq
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
$ jq -n -c -f Mutual_recursion.jq
|
||||
|
||||
[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]
|
||||
20
Task/Mutual-recursion/Jsish/mutual-recursion.jsish
Normal file
20
Task/Mutual-recursion/Jsish/mutual-recursion.jsish
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* Mutual recursion, is jsish */
|
||||
function f(num):number { return (num === 0) ? 1 : num - m(f(num - 1)); }
|
||||
function m(num):number { return (num === 0) ? 0 : num - f(m(num - 1)); }
|
||||
|
||||
function range(n=10, start=0, step=1):array {
|
||||
var a = Array(n).fill(0);
|
||||
for (var i in a) a[i] = start+i*step;
|
||||
return a;
|
||||
}
|
||||
|
||||
var a = range(21);
|
||||
puts(a.map(function (n) { return f(n); }).join(', '));
|
||||
puts(a.map(function (n) { return m(n); }).join(', '));
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
1, 1, 2, 2, 3, 3, 4, 5, 5, 6, 6, 7, 8, 8, 9, 9, 10, 11, 11, 12, 13
|
||||
0, 0, 1, 2, 2, 3, 4, 4, 5, 6, 6, 7, 7, 8, 9, 9, 10, 11, 11, 12, 12
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
2
Task/Mutual-recursion/Julia/mutual-recursion.julia
Normal file
2
Task/Mutual-recursion/Julia/mutual-recursion.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
F(n) = n < 1 ? one(n) : n - M(F(n - 1))
|
||||
M(n) = n < 1 ? zero(n) : n - F(M(n - 1))
|
||||
27
Task/Mutual-recursion/Kotlin/mutual-recursion.kotlin
Normal file
27
Task/Mutual-recursion/Kotlin/mutual-recursion.kotlin
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun f(n: Int): Int =
|
||||
when {
|
||||
n == 0 -> 1
|
||||
else -> n - m(f(n - 1))
|
||||
}
|
||||
|
||||
fun m(n: Int): Int =
|
||||
when {
|
||||
n == 0 -> 0
|
||||
else -> n - f(m(n - 1))
|
||||
}
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val n = 24
|
||||
print("n :")
|
||||
for (i in 0..n) print("%3d".format(i))
|
||||
println()
|
||||
println("-".repeat(78))
|
||||
print("F :")
|
||||
for (i in 0..24) print("%3d".format(f(i)))
|
||||
println()
|
||||
print("M :")
|
||||
for (i in 0..24) print("%3d".format(m(i)))
|
||||
println()
|
||||
}
|
||||
30
Task/Mutual-recursion/LSL/mutual-recursion.lsl
Normal file
30
Task/Mutual-recursion/LSL/mutual-recursion.lsl
Normal 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, [" "], [])));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
{def F {lambda {:n} {if {= :n 0} then 1 else {- :n {M {F {- :n 1}}}} }}}
|
||||
{def M {lambda {:n} {if {= :n 0} then 0 else {- :n {F {M {- :n 1}}}} }}}
|
||||
|
||||
{map F {serie 0 19}}
|
||||
-> 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12
|
||||
{map M {serie 0 19}}
|
||||
-> 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
{def cache
|
||||
{def cache.F {#.new}}
|
||||
{def cache.M {#.new}}
|
||||
{lambda {:f :n}
|
||||
{let { {:f :f} {:n :n}
|
||||
{:cx {if {equal? :f MF}
|
||||
then {cache.F}
|
||||
else {cache.M}}}
|
||||
} {if {equal? {#.get :cx :n} undefined}
|
||||
then {#.get {#.set! :cx :n {:f :n}} :n}
|
||||
else {#.get :cx :n}}}}}
|
||||
-> cache
|
||||
|
||||
{def MF
|
||||
{lambda {:n}
|
||||
{if {= :n 0}
|
||||
then 1
|
||||
else {- :n {MM {cache MF {- :n 1}}}}}}}
|
||||
-> MF
|
||||
|
||||
{def MM
|
||||
{lambda {:n}
|
||||
{if {= :n 0}
|
||||
then 0
|
||||
else {- :n {MF {cache MM {- :n 1}}}}}}}
|
||||
-> MM
|
||||
|
||||
{MF 80}
|
||||
-> 50 (requires 55 ms)
|
||||
|
||||
{map MF {serie 0 100}} (requires75ms)
|
||||
-> 1 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 19 20 21 21 22 22 23 24 24 25 25 26 27 27 28 29 29 30 30 31 32 32
|
||||
33 34 34 35 35 36 37 37 38 38 39 40 40 41 42 42 43 43 44 45 45 46 46 47
|
||||
48 48 49 50 50 51 51 52 53 53 54 55 55 56 56 57 58 58 59 59 60 61 61 62
|
||||
27
Task/Mutual-recursion/Liberty-BASIC/mutual-recursion.basic
Normal file
27
Task/Mutual-recursion/Liberty-BASIC/mutual-recursion.basic
Normal 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
|
||||
|
|
@ -0,0 +1,94 @@
|
|||
'// LibreOffice Basic Implementation of Hofstadter Female-Male sequences
|
||||
|
||||
'// Utility functions
|
||||
sub setfont(strfont)
|
||||
ThisComponent.getCurrentController.getViewCursor.charFontName = strfont
|
||||
end sub
|
||||
|
||||
sub newline
|
||||
oVC = thisComponent.getCurrentController.getViewCursor
|
||||
oText = oVC.text
|
||||
oText.insertControlCharacter(oVC, com.sun.star.text.ControlCharacter.PARAGRAPH_BREAK, False)
|
||||
end sub
|
||||
|
||||
sub out(sString)
|
||||
oVC = ThisComponent.getCurrentController.getViewCursor
|
||||
oText = oVC.text
|
||||
oText.insertString(oVC, sString, false)
|
||||
end sub
|
||||
|
||||
sub outln(optional sString)
|
||||
if not ismissing (sString) then out(sString)
|
||||
newline
|
||||
end sub
|
||||
|
||||
function intformat(n as integer,nlen as integer) as string
|
||||
dim nstr as string
|
||||
nstr = CStr(n)
|
||||
while len(nstr) < nlen
|
||||
nstr = " " & nstr
|
||||
wend
|
||||
intformat = nstr
|
||||
end function
|
||||
|
||||
'// Hofstadter Female-Male function definitions
|
||||
function F(n as long) as long
|
||||
if n = 0 Then
|
||||
F = 1
|
||||
elseif n > 0 Then
|
||||
F = n - M(F(n - 1))
|
||||
endif
|
||||
end function
|
||||
|
||||
function M(n)
|
||||
if n = 0 Then
|
||||
M = 0
|
||||
elseif n > 0 Then
|
||||
M = n - F(M(n - 1))
|
||||
endif
|
||||
end function
|
||||
|
||||
'// Hofstadter Female Male sequence demo routine
|
||||
sub Hofstadter_Female_Male_Demo
|
||||
'// Introductory Text
|
||||
setfont("LM Roman 10")
|
||||
outln("Rosetta Code Hofstadter Female and Male Sequence Challenge")
|
||||
outln
|
||||
out("Two functions are said to be mutually recursive if the first calls the second,")
|
||||
outln(" and in turn the second calls the first.")
|
||||
out("Write two mutually recursive functions that compute members of the Hofstadter")
|
||||
outln(" Female and Male sequences defined as:")
|
||||
outln
|
||||
setfont("LM Mono Slanted 10")
|
||||
outln(chr(9)+"F(0) = 1 ; M(0)=0")
|
||||
outln(chr(9)+"F(n) = n - M(F(n-1)), n > 0")
|
||||
outln(chr(9)+"M(n) = n - F(M(n-1)), n > 0")
|
||||
outln
|
||||
'// Sequence Generation
|
||||
const nmax as long = 20
|
||||
dim n as long
|
||||
setfont("LM Mono 10")
|
||||
out("n = "
|
||||
for n = 0 to nmax
|
||||
out(" " + intformat(n, 2))
|
||||
next n
|
||||
outln
|
||||
out("F(n) = "
|
||||
for n = 0 to nmax
|
||||
out(" " + intformat(F(n),2))
|
||||
next n
|
||||
outln
|
||||
out("M(n) = "
|
||||
for n = 0 to nmax
|
||||
out(" " + intformat(M(n), 2))
|
||||
next n
|
||||
outln
|
||||
|
||||
end sub
|
||||
|
||||
------------------------------
|
||||
Output
|
||||
------------------------------
|
||||
n = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
||||
F(n) = 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9 10 11 11 12 13
|
||||
M(n) = 0 0 1 2 2 3 4 4 5 6 6 7 7 8 9 9 10 11 11 12 12
|
||||
13
Task/Mutual-recursion/Logo/mutual-recursion.logo
Normal file
13
Task/Mutual-recursion/Logo/mutual-recursion.logo
Normal 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]
|
||||
2
Task/Mutual-recursion/Lua/mutual-recursion-1.lua
Normal file
2
Task/Mutual-recursion/Lua/mutual-recursion-1.lua
Normal 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
|
||||
3
Task/Mutual-recursion/Lua/mutual-recursion-2.lua
Normal file
3
Task/Mutual-recursion/Lua/mutual-recursion-2.lua
Normal 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
|
||||
|
|
@ -0,0 +1,76 @@
|
|||
\\ set console 70 characters by 40 lines
|
||||
Form 70, 40
|
||||
Module CheckSubs {
|
||||
Flush
|
||||
Document one$, two$
|
||||
For i =0 to 20
|
||||
Print format$("{0::-3}",i);
|
||||
f(i)
|
||||
\\ number pop then top value of stack
|
||||
one$=format$("{0::-3}",number)
|
||||
m(i)
|
||||
two$=format$("{0::-3}",number)
|
||||
Next i
|
||||
Print
|
||||
Print one$
|
||||
Print two$
|
||||
Sub f(x)
|
||||
if x<=0 then Push 1 : Exit sub
|
||||
f(x-1) ' leave result to for m(x)
|
||||
m()
|
||||
push x-number
|
||||
End Sub
|
||||
Sub m(x)
|
||||
if x<=0 then Push 0 : Exit sub
|
||||
m(x-1)
|
||||
f()
|
||||
push x-number
|
||||
End Sub
|
||||
}
|
||||
CheckSubs
|
||||
|
||||
Module Checkit {
|
||||
Function global f(n) {
|
||||
if n=0 then =1: exit
|
||||
if n>0 then =n-m(f(n-1))
|
||||
}
|
||||
Function global m(n) {
|
||||
if n=0 then =0
|
||||
if n>0 then =n-f(m(n-1))
|
||||
|
||||
}
|
||||
Document one$, two$
|
||||
For i =0 to 20
|
||||
Print format$("{0::-3}",i);
|
||||
one$=format$("{0::-3}",f(i))
|
||||
two$=format$("{0::-3}",m(i))
|
||||
Next i
|
||||
Print
|
||||
Print one$
|
||||
Print two$
|
||||
}
|
||||
Checkit
|
||||
Module Checkit2 {
|
||||
Group Alfa {
|
||||
function f(n) {
|
||||
if n=0 then =1: exit
|
||||
if n>0 then =n-.m(.f(n-1))
|
||||
}
|
||||
function m(n) {
|
||||
if n=0 then =0
|
||||
if n>0 then =n-.f(.m(n-1))
|
||||
}
|
||||
}
|
||||
Document one$, two$
|
||||
For i =0 to 20
|
||||
Print format$("{0::-3}",i);
|
||||
one$=format$("{0::-3}",Alfa.f(i))
|
||||
two$=format$("{0::-3}",Alfa.m(i))
|
||||
Next i
|
||||
Print
|
||||
Print one$
|
||||
Print two$
|
||||
Clipboard one$+{
|
||||
}+two$
|
||||
}
|
||||
Checkit2
|
||||
5
Task/Mutual-recursion/M4/mutual-recursion.m4
Normal file
5
Task/Mutual-recursion/M4/mutual-recursion.m4
Normal 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')
|
||||
57
Task/Mutual-recursion/MAD/mutual-recursion.mad
Normal file
57
Task/Mutual-recursion/MAD/mutual-recursion.mad
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
NORMAL MODE IS INTEGER
|
||||
|
||||
R SET UP STACK SPACE
|
||||
|
||||
DIMENSION STACK(100)
|
||||
SET LIST TO STACK
|
||||
|
||||
R DEFINE RECURSIVE FUNCTIONS
|
||||
R INPUT ARGUMENT ASSUMED TO BE IN N
|
||||
|
||||
INTERNAL FUNCTION(DUMMY)
|
||||
ENTRY TO FREC.
|
||||
WHENEVER N.LE.0, FUNCTION RETURN 1
|
||||
SAVE RETURN
|
||||
SAVE DATA N
|
||||
N = N-1
|
||||
N = FREC.(0)
|
||||
X = MREC.(0)
|
||||
RESTORE DATA N
|
||||
RESTORE RETURN
|
||||
FUNCTION RETURN N-X
|
||||
END OF FUNCTION
|
||||
|
||||
INTERNAL FUNCTION(DUMMY)
|
||||
ENTRY TO MREC.
|
||||
WHENEVER N.LE.0, FUNCTION RETURN 0
|
||||
SAVE RETURN
|
||||
SAVE DATA N
|
||||
N = N-1
|
||||
N = MREC.(0)
|
||||
X = FREC.(0)
|
||||
RESTORE DATA N
|
||||
RESTORE RETURN
|
||||
FUNCTION RETURN N-X
|
||||
END OF FUNCTION
|
||||
|
||||
R DEFINE FRONT-END FUNCTIONS THAT CAN BE CALLED WITH ARGMT
|
||||
|
||||
INTERNAL FUNCTION(NN)
|
||||
ENTRY TO F.
|
||||
N = NN
|
||||
FUNCTION RETURN FREC.(0)
|
||||
END OF FUNCTION
|
||||
|
||||
INTERNAL FUNCTION(NN)
|
||||
ENTRY TO M.
|
||||
N = NN
|
||||
FUNCTION RETURN MREC.(0)
|
||||
END OF FUNCTION
|
||||
|
||||
R PRINT F(0..19) AND M(0..19)
|
||||
|
||||
THROUGH SHOW, FOR I=0, 1, I.GE.20
|
||||
SHOW PRINT FORMAT FMT,I,F.(I),I,M.(I)
|
||||
VECTOR VALUES FMT =
|
||||
0 $2HF(,I2,4H) = ,I2,S8,2HM(,I2,4H) = ,I2*$
|
||||
END OF PROGRAM
|
||||
9
Task/Mutual-recursion/MATLAB/mutual-recursion-1.m
Normal file
9
Task/Mutual-recursion/MATLAB/mutual-recursion-1.m
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function Fn = female(n)
|
||||
|
||||
if n == 0
|
||||
Fn = 1;
|
||||
return
|
||||
end
|
||||
|
||||
Fn = n - male(female(n-1));
|
||||
end
|
||||
9
Task/Mutual-recursion/MATLAB/mutual-recursion-2.m
Normal file
9
Task/Mutual-recursion/MATLAB/mutual-recursion-2.m
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function Mn = male(n)
|
||||
|
||||
if n == 0
|
||||
Mn = 0;
|
||||
return
|
||||
end
|
||||
|
||||
Mn = n - female(male(n-1));
|
||||
end
|
||||
12
Task/Mutual-recursion/MATLAB/mutual-recursion-3.m
Normal file
12
Task/Mutual-recursion/MATLAB/mutual-recursion-3.m
Normal 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
|
||||
78
Task/Mutual-recursion/MMIX/mutual-recursion.mmix
Normal file
78
Task/Mutual-recursion/MMIX/mutual-recursion.mmix
Normal 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
|
||||
17
Task/Mutual-recursion/Maple/mutual-recursion.maple
Normal file
17
Task/Mutual-recursion/Maple/mutual-recursion.maple
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
female_seq := proc(n)
|
||||
if (n = 0) then
|
||||
return 1;
|
||||
else
|
||||
return n - male_seq(female_seq(n-1));
|
||||
end if;
|
||||
end proc;
|
||||
|
||||
male_seq := proc(n)
|
||||
if (n = 0) then
|
||||
return 0;
|
||||
else
|
||||
return n - female_seq(male_seq(n-1));
|
||||
end if;
|
||||
end proc;
|
||||
seq(female_seq(i), i=0..10);
|
||||
seq(male_seq(i), i=0..10);
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
f[0]:=1
|
||||
m[0]:=0
|
||||
f[n_]:=n-m[f[n-1]]
|
||||
m[n_]:=n-f[m[n-1]]
|
||||
|
|
@ -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]]
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
m /@ Range[30]
|
||||
f /@ Range[30]
|
||||
|
|
@ -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}
|
||||
23
Task/Mutual-recursion/Maxima/mutual-recursion.maxima
Normal file
23
Task/Mutual-recursion/Maxima/mutual-recursion.maxima
Normal 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)$
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue