Function to check if the head of the list can be divided by lists’ second element

x and y are already in scope by the (x : y : _) pattern. There is no need to use an extra case … of … expression. This will not work here anyway, since the case … of … expression will return either x or y. Furthermore theList not defined.

The signature is furthermore to generic. a can be of any type. But you can only calculate the mod :: Integral a => a -> a -> a on an Integral type. You thus can work with x and y directy with:

fst2Div:: Integral a => [a] -> Bool
fst2Div (x : y : _) = y `mod` x == 0
fst2Div _ = False

The second clause unifies with lists that have less than two elements. In that case we here return False.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top