We can introduce a new Elm type for the natural numbers (i.e., nonnegative integ
ID: 3776077 • Letter: W
Question
We can introduce a new Elm type for the natural numbers (i.e., nonnegative integers) with the definition type Nat = Zero | Succ Nat where the constructor Zero represents the integer value 0 and constructor Succ represents the “successor function” from mathematics. Thus (Succ Zero) denotes 1, (Succ (Succ Zero)) denotes 2, and so forth. We can define an Elm function natToInt that takes a Nat and returns the equivalent value of type Int as follows: natToInt : Nat -> Int natToInt n = case n of Zero -> 0 Succ n -> 1 + (natToInt n) Write the following Elm functions:
intToNat that takes a nonnegative Int and returns the equivalent Nat, for example, 3 returns Succ (Succ (Succ Zero)).
addNat that takes two Nat values and returns their sum as a Nat. This function cannot use the builtin integer or floating point addition.
Extend the above example by doing ONE of the following:
compNat that takes two Nat values and returns the value -1 if the first is less than the second, 0 if they are equal, and 1 if the first is greater than the second. This function cannot use the integer comparison operators.
mulNat that takes two Nat values and returns their product as a Nat. This function cannot use the builtin integer or floating point operations. (Hint: You may want to use addNat.)
Please make a note of which choice you are solving.. Thanks
Explanation / Answer
#include <iostream>
using namespace std;
class Elm {
public:
int natToInt(string nat);
string nat(int ele);
int addNat(string nat1,string nat2);
int compNat(string nat1,string nat2); //Solving first choice
};
static int z=0; //variable to keep track of results
static string nt="";
int Elm::natToInt(string nat) {
if(nat.substr(0,6)=="(Succ ") //compare the first 6 chars every time recursively
{
z=z+1; //increment the number every time
natToInt(nat.substr(6,nat.length()));
}
else
{
return z;
}
}
int len=0;
string Elm::nat(int ele) {
if(ele==0)
{
nt=nt+"Zero";
for(int i=0;i<len;i++)
{
nt=nt+")";
}
}
else
{
nt=nt+"(Succ ";
ele--;
len++;
nat(ele);
}
return " ";
}
int Elm::addNat(string nat1,string nat2) {
Elm na;
int n,m;
na.natToInt(nat1);
n=z;
z=0;
na.natToInt(nat1);
m=z;
z=m+n; //adding length of first and second
return 0;
}
int Elm::compNat(string nat1,string nat2) {
Elm na;
int n,m;
na.natToInt(nat1);
n=z; //length of first string
z=0;
na.natToInt(nat1);
m=z; //length of second string
if(n==m) //compare them
z=0;
else if(n>m)
z=1;
else
z=-1;
return 0;
}
// Main function for the program
int main() {
Elm na;
na.natToInt("(Succ (Succ (Succ Zero)))");
cout << "nat to integer : " <<z<<endl;
z=0;
na.addNat("(Succ (Succ (Succ Zero)))","(Succ (Succ (Succ Zero)))");
cout<<"sum of nats:"<<z<<endl;
z=0;
na.compNat("(Succ (Succ (Succ Zero)))","(Succ (Succ (Succ Zero)))");
cout<<"Compare:"<<z<<endl;
z=0;
na.nat(4);
cout<<nt<<endl;
nt="";
len=0;
return 0;
}
//example output:-
nat to integer : 3
sum of nats:6
Compare:0
(Succ (Succ (Succ (Succ Zero))))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.