FUNCTIONAL PROGRAMMING: Recursive function to calculate the rest of the division between two positive integers in ELM

Asked

Viewed 453 times

0

Hello, I’m having trouble making a recursive function that passes as a result the rest of the division of two integers into ELM. I was able to perform the function without recursion as shown in the print below:

inserir a descrição da imagem aqui

But I need to apply a recursive function that has the same result ! Could you help me ?! The language used is Haskell-derived ELM !

1 answer

2


The key point to construct the recursive function in this case is to employ the subtraction operation:

restoPosRec dividendo divisor =
if dividendo == 0 || divisor == 1 then 0
else if dividendo < divisor then dividendo
else restoPosRec (dividendo - divisor) divisor

Note: I suggest that in the iterative function of the question (restPos) change the line else if divisor > dividendo then 0 for else if divisor > dividendo then dividendo, otherwise the function always returns 0 if in the initial call the dividend is less than the divisor. For example in restoPos 2 3 = 0, when it should be restoPos 2 3 = 2.

  • 1

    Thank you very much friend ! Your reply was succinct and direct

  • 1

    Thank you @Fehlbergskady!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.