首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >丢弃你的样板文件

丢弃你的样板文件
EN

Stack Overflow用户
提问于 2014-11-07 15:13:51
回答 1查看 121关注 0票数 0

关于SYB,我还是个初学者。

我试着编写一段代码来获得表达式中的变量

代码语言:javascript
复制
 variables = removeDuplicate $ (everything (++)  ([] `mkQ` f))
          where
             f (ExprVar st) = [st]
             f _  = []

其中removeDuplicate消除了列表中的重复变量

ExprVar是我的数据类型Datatype还包括用于加法、减法乘法、除法和数字的ExprAddExprsubExprMulExprDivExprNum

我在编译时遇到以下错误:

代码语言:javascript
复制
No instance for (Data a0) arising from a use of `everything'
    The type variable `a0' is ambiguous
    Possible cause: the monomorphism restriction applied to the following:
      variables :: a0 -> [[Char]] (bound at ParserExpr.hs:107:1)
    Probable fix: give these definition(s) an explicit type signature
                  or use -XNoMonomorphismRestriction
    Note: there are several potential instances:
      instance Data Expr -- Defined at ParserExpr.hs:24:28
      instance (Data a, Data b) => Data (a -> b)
        -- Defined in `Data.Generics.Instances'
      instance Data DataType -- Defined in `Data.Generics.Instances'
      ...plus 43 others
    In the expression: (everything (++) ([] `mkQ` f))
    In an equation for `variables':
        variables
          = (everything (++) ([] `mkQ` f))
          where
              f (ExprVar st) = [st]
              f _ = []

请让我知道我哪里错了?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2014-11-07 15:50:48

错误消息准确地指出了问题所在,并提供了两种可能的解决方案,这两种方案都有效:

在GHC中添加Data a => a -> [String] as type signature

  • enable -XNoMonomorphismRestriction标志(或添加LANGUAGE杂注)

有关更多信息,请参阅https://www.haskell.org/haskellwiki/Monomorphism_restriction

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

https://stackoverflow.com/questions/26795997

复制
相关文章

相似问题

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