Haskell, division without div

1

Hello, I have a problem in hand, try to make a code in Haskell with the following conditions:

We introduce two numbers a y b to be divided a/b of type Integer , so that we take your module and your coscious one

With the following restrictions you can only use sums, subtractions and comparators you can not use neither div , nor mod , or anything

// 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'm returning the rest, but I have no way, well rather I do not know how to get that cosciente (this right now or observe that is divided between 0 , or that x<y in a base case

You just have to split things like 3/2 10/4, but nothing of 3/8.

Suggestions of all kinds are accepted, I am starting and all help is good.

Greetings and thanks

    
asked by Ramosaurio 06.10.2016 в 18:32
source

1 answer

2

It could be done by introducing an auxiliary function that would drag the value of the quotient in each recursive iteration.

cocienteYResto :: Integer -> Integer -> (Integer, Integer)
cocienteYResto = aux 0
    where aux c x y
            | x >= y    = aux (c+1) (x-y) y
            | otherwise = (c,x)

But, since we are trying to

answered by 07.10.2016 / 01:00
source