I'm getting a type error because I'm trying to recursively add a Maybe int int the function rankP seen bellow. Because the return type of rankP is Maybe Int, and the type of rankC is a tuple of Ints, it is telling me I cannot add Int with Maybe Int.
type Prog = [Cmd]
data Cmd = LD Int
| ADD
| MULT
| DUP
| INC
| SWAP
| POP Int
deriving Show
type Stack = [Int]
type D = Stack -> Maybe Stack
type Rank = Int
type CmdRank = (Int,Int)
rankC :: Cmd -> CmdRank
rankC (LD _) = (0,1)
rankC ADD = (2,1)
rankC MULT = (2,1)
rankC DUP = (1,2)
rankC INC = (1,1)
rankC SWAP = (2,2)
rankC (POP x) = (x,0)
rankP :: Prog -> Maybe Rank
rankP [] = Nothing
rankP [x] = Just (snd(rankC x) - fst(rankC x))
rankP (x:xs) = if ((Just (snd(rankC x) - fst(rankC x)) + rankP xs) < 0) then Nothing
else Just (snd(rankC x) - fst(rankC x)) + (rankP xs)
Here is the error I am receiving:
hw3.hs:43:64:
Couldn't match expected type `Int' with actual type `Maybe Rank'
In the return type of a call of `rankP'
In the first argument of `Just', namely `(rankP xs)'
In the second argument of `(+)', namely `Just (rankP xs)'
Failed, modules loaded: none.