【问题标题】:"‘Node’ is applied to too few arguments" even though there are exact number“‘节点’应用于太少的参数”即使有确切的数字
【发布时间】:2019-04-16 21:22:55
【问题描述】:

sa 是类型变量。 在构造函数中,前两个参数是数据,然后是它的父级,它在图中的级别,然后是它的子级列表。

data Node s a = Root | Node s a (Node s a) Int [Node s a]
createRoot :: (ProblemState s a) => s-> a -> Node s a
createRoot state act= Node (state act Root 0 [])

我已经将完全相同数量的 5 个参数传递给 Node 构造函数,但是我遇到了错误。

• Couldn't match expected type ‘Node s a’
              with actual type ‘a1
                                -> Node s1 a1 -> Int -> [Node s1 a1] -> Node s1 a1’
• Probable cause: ‘Node’ is applied to too few arguments
  In the expression: Node (state act Root 0 [])
  In an equation for ‘createRoot’:
      createRoot state act = Node (state act Root 0 [])
• Relevant bindings include
    act :: a (bound at Search.hs:43:24)
    state :: s (bound at Search.hs:43:18)
    createRoot :: s -> a -> Node s a (bound at Search.hs:43:1)

【问题讨论】:

  • 对我来说就像一个包围错误。 Node (state act Root 0 []) 表示您给Node 构造函数1 参数,其值为state act Root 0 []。我相信你的意思是没有括号的:Node state act Root 0 [],这就是你如何将 5 个参数应用于 Haskell 中的函数。
  • Haskell 函数被称为fun arg1 arg2 arg4,而不是fun (arg1 arg2 arg3)——后者读作fun(arg1(arg2, arg3))

标签: haskell syntax function-call


【解决方案1】:

括号用于对表达式进行分组。 (length "hello" + 2) 是单个值,而不是 4。

同样,Node (...)Node 应用于单个参数:(state act Root 0 [])。显然这是错误的(并且需要 state 是一个接受四个参数的函数)。

解决办法就是去掉括号:

Node state act Root 0 []

现在Node 应用于五个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-25
    • 2017-04-01
    • 1970-01-01
    • 2021-05-14
    相关资源
    最近更新 更多