I am new to Haskell and I couldnt understand the "Null $ filter" thing.
isPrime n
| n < 2 = error "Zu kleine Zahl fuer Primzahltest"
| otherwise = null $ filter (\k -> n `mod` k == 0) [2.. n-1]
I am new to Haskell and I couldnt understand the "Null $ filter" thing.
isPrime n
| n < 2 = error "Zu kleine Zahl fuer Primzahltest"
| otherwise = null $ filter (\k -> n `mod` k == 0) [2.. n-1]
The
($) :: (a -> b) -> a -> boperator applies the left operand to the right operand. Because it has a low precedence, it is used as a way to "group" expressions. The expression is thus equivalent to:null :: Foldable f => f a -> Boolis a function that checks whether aFoldablehas no elements. So for a list, it will returnTrueif the list is empty, andFalseotherwise.The list contains the integers
kbetween2andn-1in ascending order wheren `mod` kis0, hence the divisors ofn. By usingnullwe thus check that the numbernhas no divisors, and if that is the case we returnTrue; otherwise we returnFalse.