I am solving a problem in which I have to count all the even numbers in an array in purescript. I have written down code but I am facing type mismatch error.
import Data.Array (null)
import Data.Array.Partial (tail,head)
import Partial.Unsafe (unsafePartial)
import Math
iseven :: Int -> Boolean
iseven a = mod a 2 == 0
len :: forall a. Array a -> Int
len arr =
if null arr
then 0
else
if iseven unsafePartial head arr
then 1 + len (unsafePartial tail arr)
else len (unsafePartial tail arr)
But I am getting an error.
Error found:
in module $PSCI
at :6:18 - 6:40 (line 6, column 18 - line 6, column 40)
Could not match type
a1
with type
Int
while checking that type t0
is at least as general as type Int
while checking that expression (unsafePartial head) arr
has type Int
in binding group len
where a1 is a rigid type variable
bound at (line 0, column 0 - line 0, column 0)
t0 is an unknown type
I am new to purescript so I am not able to understand the error.
iseven unsafePartial head arr
is incorrect. Judging by the error you get, that line probably looks something likeiseven (unsafePartial head arr)
. Please double check. – Fyodor Soikiniseven (unsafePartial head arr)
, which is not what you posted in the question. – Fyodor Soikin