我想做Maybe Substitution -> Maybe Substitution -> Maybe Substitution where type Substitution = [(Variable,Terme)],但是当我使用++时,我有这个
/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.发布于 2014-11-16 04:51:22
(++)适用于列表,而不是Maybe的,您需要提升它
下面是如何让它在Maybe String上工作。
import Control.Monad
(liftM2 (++)) (Just "aa") (Just "bb")您可以通过定义一个新的运算符来使其看起来更好。
(+++) = liftM2 (++)然后像这样使用它
Just "aa" +++ Nothing发布于 2014-11-16 04:54:45
Maybe是一个应用程序,因此您可以使用诸如liftA2之类的函数将函数应用于Maybe中的值。
例如(如果您的变量和术语是字符串):
import Control.Applicative
liftA2 (++) (Just [("foo", "bar")]) (Just [("FOO", "BAR")])
-- Just [("foo","bar"),("FOO","BAR")]或者等效地使用<$>和<*>中缀运算符:
(++) <$> Just [("foo", "bar")] <*> Just [("FOO", "BAR")]
-- Just [("foo","bar"),("FOO","BAR")]请参阅http://learnyouahaskell.com/functors-applicative-functors-and-monoids
https://stackoverflow.com/questions/26950523
复制相似问题