How to resolve the algorithm Mutual recursion step by step in the Picat programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Mutual recursion step by step in the Picat programming language
Table of Contents
Problem Statement
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 Hofstadter Female and Male sequences defined as:
(If a language does not allow for a solution using mutually recursive functions then state this rather than give a solution by other means).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Mutual recursion step by step in the Picat programming language
Source code in the picat programming language
table
f(0) = 1.
f(N) = N - m(f(N-1)), N > 0 => true.
table
m(0) = 0.
m(N) = N - f(m(N-1)), N > 0 => true.
table
female(0,1).
female(N,F) :-
N>0,
N1 = N-1,
female(N1,R),
male(R, R1),
F = N-R1.
table
male(0,0).
male(N,F) :-
N>0,
N1 = N-1,
male(N1,R),
female(R, R1),
F = N-R1.
go =>
N = 30,
println(func),
test_func(N),
println(pred),
test_pred(N),
nl.
nl.
% Testing the function based approach
test_func(N) =>
println([M : I in 0..N, male(I,M)]),
println([F : I in 0..N, female(I,F)]),
nl.
% Testing the predicate approach
test_pred(N) =>
println([M : I in 0..N, male(I,M)]),
println([F : I in 0..N, female(I,F)]),
nl.
You may also check:How to resolve the algorithm Joystick position step by step in the Locomotive Basic programming language
You may also check:How to resolve the algorithm Taxicab numbers step by step in the C# programming language
You may also check:How to resolve the algorithm Repeat a string step by step in the zig programming language
You may also check:How to resolve the algorithm Search a list step by step in the Io programming language
You may also check:How to resolve the algorithm 99 bottles of beer step by step in the C programming language