Help with countEven - ch4 of updated purescript book

  1. (Medium) Write a recursive function countEven which counts the number of even integers in
    an array. Hint: the function head (also available in Data.Array ) can be used to find the first
    element in a non-empty array.

I can’t figure out how to get my solution to type check. The result of head is Maybe a, but fromMaybe doesn’t seem to know what type it should extract to.

countEven :: Array Int -> Int
countEven arr =
    if null arr
        then 0
        --else if (2 `mod` 2) == 0
        --else if (fromMaybe [] $ head $ arr `mod` 2) == 0
        --else if ((head arr) `mod` 2) == 0
        else if ((fromMaybe [] (head arr)) `mod` 2) == 0
                then 1 + (countEven $ fromMaybe [] $ tail arr)
                else        countEven $ fromMaybe [] $ tail arr

Looking at the type signature for fromMaybe:

fromMaybe :: forall a. a -> Maybe a -> a

it looks like you’re using fromMaybe like this:

  result :: Maybe Int
  result = head arr

  usage = fromMaybe ([] :: forall anyType. Array anyType) result

I’m guessing the compiler is wondering how to unify Array anyType with Int, correct?

If you just write [], the compiler doesn’t know if that is an empty array of Ints or Strings or some other type. So, it’s likely asking you to further annotate the value with a type since it cannot infer it. Some thing like ([] :: Array Int).

Note: I’m only providing clarification on your question, not on how to solve this problem.

fromMaybe is a function that ‘extracts’ a purescript value that’s wrapped in a Maybe.

Here’s how we might implement this naively:

fromMaybe :: forall a. Maybe a -> a
fromMaybe (Just value) = value {- value has type a -}
fromMaybe Nothing      = {- Uhm? How to return something of type a? -}

Right now, this is a partial function. It can’t cover all its inputs. The problem is that we don’t have anything to return in the case where Maybe a doesn’t have a value.

One solution is to upgrade this to take a default value with the right type.

fromMaybe :: forall a. a -> Maybe a -> a
fromMaybe fallback (Just value) = value    {- value has type a -}
fromMaybe fallback Nothing      = fallback {- fallback has type a -}

Back to your code. Consider the types here:

fromMaybe [] (head arr)

[] is an Array
head arr is Maybe Int

You’re asking fromMaybe to return an Int, and if it can’t, to return an empty array.

That would type something like this:

fromMaybe :: forall a b. a -> Maybe b -> Either a b
fromMaybe fallback (Just value) = Right value   {- value has type b -}
fromMaybe fallback Nothing      = Left fallback {- fallback has type a -}

arr :: Array Int
arr = ...

firstValue :: forall a. Either (Array a) Int
firstValue = fromMaybe [] (head arr)

Instead of the one we created here, fromMaybe has this type signature:

fromMaybe :: forall a. a -> Maybe a -> a

Instead of having two types (a and b), we have only one. Which tells us that the default value and the value being wrapped in the Maybe must have the same type.

[] and Int do not have the same type.

Instead, you could try something like:

fromMaybe 0 (head arr)

In this case, (0 :: Int) and ((head arr) :: Maybe Int) have types that work with fromMaybe.


Thanks. This makes complete sense now. Of course it’s looking for an Int… and the default value of Array makes zero sense then.