data BaconOrIndex = Bacon | Indx Int deriving (Eq,Ord,Show)
import Data.Maybe
whereIsBM = whereIsBM' 1
whereIsBM' _ Empty = Nothing
whereIsBM' !n (Cons Bacon _) = Just n
whereIsBM' !n (Cons _ lx) = whereIsBM' (succ n) lx
> whereIsBM (Cons (Indx 5) (Cons (Indx 13) (Cons (Indx 2) (Cons (Indx 8) Empty))))
Nothing
> whereIsBM (Cons (Indx 5) (Cons (Indx 13) (Cons Bacon (Cons (Indx 8) Empty))))
Just 3
to work. Unfortunately, I couldn't get this
whereIsBM boiList = go 0
where
go !_ Empty = Nothing
go !acc (Cons idx lx) | (idx == Bacon) = Just acc
| otherwise = go (acc + 1) lx
to work. Both are nearly identical, but the latter gives this error
> whereIsBM (Cons (Indx 5) (Cons (Indx 13) (Cons (Indx 2) (Cons (Indx 8) Empty))))
No instance for (Show (MyList BaconOrIndex -> Maybe Integer))
: arising from a use of `print'
This also failed
whereIsBM boiList = case boiList of
Nothing -> Nothing
Just (Cons idx lx)
| (idx == Bacon) -> Just 1
| otherwise -> (1 +) <$> (whereIsBM lx)
Couldn't match type `Maybe (MyList BaconOrIndex)'
with `MyList BaconOrIndex'
Expected type: MyList BaconOrIndex -> Maybe a
Actual type: Maybe (MyList BaconOrIndex) -> Maybe a
Not sure why this didn't work. Would like to understand the whole fmap idea as applied here, though.