Jump to content

Mutual recursion: Difference between revisions

→‎{{header|CLU}}: Correct and clarify the Clu example. No special tricks are needed for mutual recursion, it's standard practice to "spec" the clu file before compiling it.
m (syntax highlighting fixup automation)
(→‎{{header|CLU}}: Correct and clarify the Clu example. No special tricks are needed for mutual recursion, it's standard practice to "spec" the clu file before compiling it.)
Line 1,049:
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">% AtTo thedeclare topthings level, definitionsyou can onlyeither seewrite thean definitions above.spc file or you can use
% the clu file itself as a specfile. For a small program a common
% But if we put F and M in a cluster, they can see each other.
% idiom is to spec and compile the same source file:
mutrec = cluster is F, M
%
rep = null
% pclu -spec mutrec.clu -clu mutrec.clu
%
F = proc (n: int) returns (int)
start_up = proc ()
if n=0 then return(1)
else returnprint_first_16(n"F", - M(F(n-1)))
print_first_16("M", M)
end
end Fstart_up
 
M = proc (n: int) returns (int)
if n=0 then return(0)
else return(n - F(M(n-1)))
end
end M
end mutrec
 
% If we absolutely _must_ have them defined at the top level,
% we can then just take them out of the cluster.
F = mutrec$F
M = mutrec$M
 
% Print the first few values for F and M
Line 1,076 ⟶ 1,064:
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
 
start_upF = proc (n: int) returns (int)
if n = 0 then return(1)
print_first_16("F", F)
return (1)
print_first_16("M", M)
endelse
end start_up</syntaxhighlight>
return (n - M(F(n-1)))
end
end F
 
FM = proc (n: int) returns (int)
if n = 0 then return(0)
return (0)
else
return (n - F(M(n-1)))
end
end M
end start_up</syntaxhighlight>
{{out}}
<pre>F: 1 1 2 2 3 3 4 5 5 6 6 7 8 8 9 9
3

edits

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