Haskell input: passing a number that looks fractional, but will always be Integer (anti-aliasing type)

I am currently studying Haskell using the Gentle Introduction to the Haskell website , and I took a break halfway to section 4 to test my knowledge. I am trying to implement the "greatest prime number" function that I used when I was working in C, but I have problems with the Haskell dialing system. I am trying to pass a number that looks like a fractional Int, but because I used the module to check if it is divisible, I know it will evaluate Int. Here's the context:

C: I super-commented on this in case this is unclear, but the code should be pretty simple.

int highest(long currDom, long lastLargest, long currMax) /* This is a recursive function that starts at the lowest prime number, 2, * and divides into currMax. If a division operation is even - modulus returns 0 - * then the prime number in the division is saved as "lastLargest," and the * function calls itself again, with MAX now passed as MAX/lastLargest. Otherwise, * the function is called with currMax remaining the same value, and the * current denominator to try (currDom) incremented by one. */ { if (currDom > currMax) //end result - when the current value of MAX is less return lastLargest; //than the value of the denominator we're trying, we're done else { if (currMax % currDom == 0) //if modulus succeeds, try again with Max/currDom return highest(currDom, currDom, currMax/currDom); //denominator is kept the same incase else //it goes into MAX multiple times -eg 2 into 8 return highest(currDom+1, lastLargest, currMax); //else, try the next denominator. } } 

If you were looking for the highest result in 10, for example, you would name it by indicating "the highest (10, 2, 1)" - you are looking for the highest prime number in 10, starting at 2, and the current highest number in the number is 1. It will return when it repeats the number 5 as a divisor a second time and sees that curDom is now 1.

The problem is that when I try to do this in Haskell, on the fourth line in my code, I run into the problem of passing a number divided by a prime that goes into it - it seems to be a fractional Int, but because I already checked with the module, I know that it is going to allow regular Int. Here is the code I'm working with:

 greatestPrime :: Int -> Int -> Int -> Int greatestPrime num curPrime greatest | (curPrime > num) = greatest greatestPrime num curPrime greatest | (mod num curPrime) > 0 = greatestPrime num (curPrime+1) greatest greatestPrime num curPrime greatest | (mod num curPrime) == 0 = greatestPrime (num/curPrime) curPrime greatest 

If you tried to get the highest initial digit at 10, for example, you would call it with "maximumPrime 10 2 1", so you start the search at 2 and your current largest prime is 1.

I would appreciate any help with this - either using type aliases, implementing common code, or even with syntax / code locking. I'm new to haskell, so there may be a way to write this that makes more sense; however, I am not looking for a complete rewrite algorithm like a sieve. Thank you for your time.

+4
source share
1 answer

The / operator is of type (/) :: Fractional a => a -> a -> a , which means that it only works with Fractional types of type Float , Double and Rational , and not integers.

Use div :: Integral a => a -> a -> a for integer division.

 > 10 `div` 2 5 > 7 `div` 2 3 

There's also a quot , which is rounded to zero instead of negative infinity:

 > (-7) `div` 2 -4 > (-7) `quot` 2 -3 
+11
source

Source: https://habr.com/ru/post/1399855/


All Articles