I am doing the assignment in the book "PureScript by example" to use recursion to count the number of even items in an array.
Here is the code I wrote
isEven :: Int -> Boolean
isEven 0 = true
isEven 1 = false
isEven n = isEven(n - 2)
countEven :: List Int -> Int
countEven list =
if null list
then 0
else
if isEven(unsafePartial (head list))
then 1
else 0 + countEven(unsafePartial (tail list))
I get an error message saying
Error found:
in module Main
at src/Main.purs line 74, column 17 - line 74, column 42
Could not match type
Maybe Int
with type
Int
while checking that type t0
is at least as general as type Int
while checking that expression unsafePartial (head list)
has type Int
in binding group countEven
I am a little surprised by this because the data types ofunsafePartial head list is Int and unsafePartial tail list is List Int
So why does it feel that there is a Maybe somewhere?