Hello good I have a problem in hand try to make a code in Haskell with the following conditions:
We introduce two numbers a y b
for it to be divided a/b
by type Integer
, so that we have its module and its quotient
With the following restrictions only additions, subtractions and comparators can be used neither div
, nor mod
, nor anything can be used
//Edited from here
cocienteYResto:: Integer -> Integer -> (Integer, Integer)
cocienteYResto x y
| x < y = (x,?????)
| otherwise = ((cocienteYResto (x - y) y))
well here as we see when x< y
I am returning the rest, but I have no way, well rather I do not know how to get that quotient (this right now does not even observe that it is divided by 0
, nor that x<y
in a base case
you just have to divide things like 3/2 10/4, but nothing 3/8 .
Suggestions of all kinds are accepted, I'm starting and any help is good for me.
Greetings and thank you
It could be done by introducing an auxiliary function that drags the value of the quotient in each recursive iteration.
But, since we're dealing with functional-programming , we can take advantage of what we have to make it more elegant:
A better version: