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 -> b
operator 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 $ filter (\k -> n `mod` k == 0) [2.. n-1]
null ( filter (\k -> n `mod` k == 0) [2.. n-1] )
null :: Foldable f => f a -> Bool
is a function that checks whether a Foldable
has no elements. So for a list, it will return True
if the list is empty, and False
otherwise.
The list contains the integers k
between 2
and n-1
in ascending order where n `mod` k
is 0
, hence the divisors of n
. By using null
we thus check that the number n
has no divisors, and if that is the case we return True
; otherwise we return False
.