2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -2,6 +2,7 @@ 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 \\
@ -9,6 +10,8 @@ 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>

View file

@ -1,21 +1,19 @@
cat mutual_recursion.awk:
#!/usr/local/bin/gawk -f
# User defined functions
function F(n)
{
if ( n == 0 ) return 1;
return n - M(F(n-1))
}
{ return n == 0 ? 1 : n - M(F(n-1)) }
function M(n)
{
if ( n == 0 ) return 0;
return n - F(M(n-1))
}
{ return n == 0 ? 0 : n - F(M(n-1)) }
BEGIN {
for(i=0; i < 20; i++) {
for(i=0; i <= 20; i++) {
printf "%3d ", F(i)
}
print ""
for(i=0; i < 20; i++) {
for(i=0; i <= 20; i++) {
printf "%3d ", M(i)
}
print ""

View file

@ -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

View file

@ -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}}

View file

@ -1,3 +1,4 @@
cat mutual_recursion.bc:
define f(n) {
if ( n == 0 ) return(1);
return(n - m(f(n-1)));

View file

@ -7,3 +7,4 @@ for(i=0; i < 19; i++) {
print m(i); print " ";
}
print "\n";
quit

View file

@ -0,0 +1 @@
var range = (m, n) => Array(... Array(n - m + 1)).map((x, i) => m + i)

View file

@ -1,10 +1,10 @@
/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
parse arg lim .; if lim='' then lim=40; w=length(lim); pad=left('',20)
/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */
parse arg lim .; if lim='' then lim=40; w=length(lim); pad=left('', 20)
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
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))
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) )

View file

@ -1,14 +1,14 @@
/*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=
/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */
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
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(j),w)
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
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

View file

@ -1,17 +1,17 @@
/*REXX program shows mutual recursion (via Hofstadter Male & Female sequence).*/
/*────────If LIM is negative, a single result is shown for the abs(lim) entry.*/
/*REXX program shows mutual recursion (via the Hofstadter Male and Female sequences). */
/*───────────────── 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)
w=length(aLim); $m.=.; $m.0=0; $f.=.; $f.0=1; Js=; Fs=; Ms=
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
Js=Js right(j,w); Fs=Fs right(F(j),w); Ms=Ms right(M(j),w)
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 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
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

View file

@ -0,0 +1,22 @@
% Forward definitions: [also deletes any existing definition]
define f();
define m();
define f(n) {
if (n == 0) return 1;
else if (n < 0) error("oops");
return n - m(f(n - 1));
}
define m(n) {
if (n == 0) return 0;
else if (n < 0) error("oops");
return n - f(m(n - 1));
}
foreach $1 ([0:19])
() = printf("%d ", f($1));
() = printf("\n");
foreach $1 ([0:19])
() = printf("%d ", m($1));
() = printf("\n");

View file

@ -1,13 +1,12 @@
@(do
(defun f (n)
(if (>= 0 n)
1
(- n (m (f (- n 1))))))
(defun f (n)
(if (>= 0 n)
1
(- n (m (f (- n 1))))))
(defun m (n)
(if (>= 0 n)
0
(- n (f (m (- n 1))))))
(defun m (n)
(if (>= 0 n)
0
(- n (f (m (- n 1))))))
(each ((n (range 0 15)))
(format t "f(~s) = ~s; m(~s) = ~s\n" n (f n) n (m n))))
(each ((n (range 0 15)))
(format t "f(~s) = ~s; m(~s) = ~s\n" n (f n) n (m n)))