首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >haskell how to [a]++ [b] with b

haskell how to [a]++ [b] with b
EN

Stack Overflow用户
提问于 2014-11-16 04:43:55
回答 2查看 61关注 0票数 0

我想做Maybe Substitution -> Maybe Substitution -> Maybe Substitution where type Substitution = [(Variable,Terme)],但是当我使用++时,我有这个

代码语言:javascript
复制
/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.
EN

回答 2

Stack Overflow用户

发布于 2014-11-16 04:51:22

(++)适用于列表,而不是Maybe的,您需要提升它

下面是如何让它在Maybe String上工作。

代码语言:javascript
复制
import Control.Monad
(liftM2 (++)) (Just "aa") (Just "bb")

您可以通过定义一个新的运算符来使其看起来更好。

代码语言:javascript
复制
(+++) = liftM2 (++)

然后像这样使用它

代码语言:javascript
复制
Just "aa" +++ Nothing
票数 1
EN

Stack Overflow用户

发布于 2014-11-16 04:54:45

Maybe是一个应用程序,因此您可以使用诸如liftA2之类的函数将函数应用于Maybe中的值。

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

代码语言:javascript
复制
import Control.Applicative

liftA2 (++) (Just [("foo", "bar")])  (Just [("FOO", "BAR")])
-- Just [("foo","bar"),("FOO","BAR")]

或者等效地使用<$><*>中缀运算符:

代码语言:javascript
复制
(++) <$> Just [("foo", "bar")]  <*> Just [("FOO", "BAR")]
-- Just [("foo","bar"),("FOO","BAR")]

请参阅http://learnyouahaskell.com/functors-applicative-functors-and-monoids

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26950523

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档