haskell如何[a]++;[b] 也许

haskell如何[a]++;[b] 也许,haskell,Haskell,我想做Maybe Substitution->Maybe Substitution->Maybe Substitution 其中类型替换=[(变量,术语)]但是当我使用++时,我有这个 /Users/michel/Documents/workspace/2LammensMichelInterpreteurProlog/Setup.hs:65:58: Couldn't match expected type ‘[a0]’ with actual type ‘

我想做
Maybe Substitution->Maybe Substitution->Maybe Substitution
其中
类型替换=[(变量,术语)]
但是当我使用
++
时,我有这个

/Users/michel/Documents/workspace/2LammensMichelInterpreteurProlog/Setup.hs:65:58:
    Couldn't match expected type ‘[a0]’
                with actual type ‘Maybe Substitution’
    In the first argument of ‘(++)’, namely ‘listsub’
    In the expression: listsub ++ listsub

/Users/michel/Documents/workspace/2LammensMichelInterpreteurProlog/Setup.hs:65:58:
    Couldn't match expected type ‘Maybe Substitution’
                with actual type ‘[a0]’
    In the expression: listsub ++ listsub
    In an equation for ‘substition’:
        substition ((V variable), (F nom1 lTerme1)) listsub
          = listsub ++ listsub

/Users/michel/Documents/workspace/2LammensMichelInterpreteurProlog/Setup.hs:65:69:
    Couldn't match expected type ‘[a0]’
                with actual type ‘Maybe Substitution’
    In the second argument of ‘(++)’, namely ‘listsub’
    In the expression: listsub ++ listsub
Failed, modules loaded: none.
(++)
在列表上工作,而不是
可能
,您需要将其抬起

下面是如何让它在
可能是String
上工作

import Control.Monad
(liftM2 (++)) (Just "aa") (Just "bb")
您可以通过定义一个新的操作符使它看起来更漂亮

(+++) = liftM2 (++)
然后像这样使用它

Just "aa" +++ Nothing

Maybe
是一个应用程序,因此您可以使用
liftA2
等函数将函数应用于
Maybe
s中的值

例如(如果变量和术语是字符串):

或者等效地,使用
中缀运算符:

(++) <$> Just [("foo", "bar")]  <*> Just [("FOO", "BAR")]
-- Just [("foo","bar"),("FOO","BAR")]
(++)Just[(“foo”,“bar”)]Just[(“foo”,“bar”)]
--只是[(“foo”,“bar”),(“foo”,“bar”)]

请参见

根据错误,您试图连接两个对象,但其中至少有一个不是列表。在使用
++
之前,请检查以确保它们是列表。
(++) <$> Just [("foo", "bar")]  <*> Just [("FOO", "BAR")]
-- Just [("foo","bar"),("FOO","BAR")]