我收到了类型错误,因为我试图递归地添加一个Maybe int int函数rankP,如下所示。因为rankP的返回类型是Maybe Int,而rankC的类型是Ints的元组,它告诉我我不能用Maybe Int添加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)
以下是我收到的错误:
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.
答案 0 :(得分:1)
我的错误略有不同:
No instance for (Num (Maybe Int)) arising from a use of ‘+’
In the first argument of ‘(<)’, namely
‘(Just (snd (rankC x) - fst (rankC x)) + rankP xs)’
In the expression:
((Just (snd (rankC x) - fst (rankC x)) + rankP xs) < 0)
In the expression:
if ((Just (snd (rankC x) - fst (rankC x)) + rankP xs) < 0) then
Nothing
else
Just (snd (rankC x) - fst (rankC x)) + (rankP xs)
但请注意
(Just (snd(rankC x) - fst(rankC x)) + rankP xs) < 0
您尝试在+
个Maybe Rank
对象上使用<
,并将结果与Maybe Rank
进行比较为0.这些都不起作用。
从您的代码中,您似乎试图“提取”SELECT form_user.email
FROM form_instance JOIN
form_user
ON form_instance.form_user_id = form_user.form_user_id
WHERE form_instance.appointment_date >= '2017-04-13' AND <= '2017-05-11' AND
form_instance.status = 3
的值;请参阅this question。
答案 1 :(得分:0)
问题在于,在进行任何数学运算之前,如果确实需要从Maybe Rank
中提取Rank
,则尝试使用Maybe Rank
进行算术运算。一种方法是直接手工完成:
rankP (x:xs) = if y == Nothing
then Nothing
else let Just y' = y in
if snd(rankC x) - fst(rankC x) + y' < 0
then Nothing
else Just (snd(rankC x) - fst(rankC x) + y')
where y = rankP xs
当你开始理解Maybe
的monadic属性时,肯定会有更优雅的方法。我还没有在这些方面提供太多帮助。