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

Viewed 75

I've got this piece of code that is supposed to check whther or not first elemnt of an array can be divided by the seocnd one. Have been struggling for some time now with finding the solution, as after compiling i get this error:

Parse error (line 2, column 22): parse error (possibly incorrect indentation or mismatched brackets)

Indentation is quite confusing to me as I'm quite new to Haskell, any help would be appreciated with determining what should I do here

fst2Div:: [a] -> Bool
fst2Div (x : y : _) =
case theList of (x:_:_) -> x
                (_:y:_) -> y
if (y `mod` x)==0 then True
                    else  False
1 Answers

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.

Related