Rewrite the following functions and predicates in Java or C++, using recursion i
ID: 3937415 • Letter: R
Question
Rewrite the following functions and predicates in Java or C++, using recursion if possible. %%% Arithmetic functions and relations succ(X) = X+1. pred(X) = X-1. add(0, Y) = Y. add(X, Y) = succ(add(pred(X), Y)). sub(X, 0) = X. sub(X, Y) = sub(pred(X), pred(Y)). mul(0, _) = 0. mul(X, Y) = add(mul(pred(X), Y), Y). % name it my_div, because div/2 is a built-in in Picat my_div(X, Y) = 0, 1t(X, Y) => true. my_div(X, Y) = succ(my_div(sub(X, Y), Y)). % rem/2 is a built-in function my_rem(X, Y) = X, 1t(X, Y) => true. % if X fail. le(X, Y) => 1e(pred(X), pred(Y)). % X 1e(succ(X)>Y). gt(X, Y) => 1t(Y, X). ge(X, Y) => 1e(Y, X). eq(X, Y) => ge(X, Y), ge(Y, X). %%% Functions and predicates on lists.Explanation / Answer
#include<iostream.h>
#inlcude<conio.h>
int succ(int x)
{
return x+1;
}
int pred(int x)
{
return x-1;
}
int add(int x,int y);
{
int result;
if(x==0)
return y;
else
{result = succ(add(pred(x),y));
return result;}
}
int sub(int x, int y)
{ int result;
if(y==0)
return x;
else
{
result = sub(pred(x),pred(y));
return result;
}
}
int mul(int x, int y)
{ int result;
if(y==0)
return 0;
else
{
result = add(mul(pred(x),y),y);
return result;
}
}
void main()
{ int result1, result2, result3;
cout<<"Please enter value x and y";
cin>>x>>y;
result1 = add(x,y);
result2 = sub(x,y);
result3 = mul(x,y);
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.