简体   繁体   English

Haskell:列表理解:约束中的非类型变量参数:Num [t]

[英]Haskell: list comprehension: Non type-variable argument in the constraint: Num [t]

When I am trying to run following function, I am getting error. 当我尝试运行以下功能时,出现错误。

Prelude> let squareSum list = [result | (x, y, z) <- list, result <- x^2 + y^2 + z^2] 

<interactive>:4:5:
    Non type-variable argument in the constraint: Num [t]
    (Use FlexibleContexts to permit this)
    When checking that ‘squareSum’ has the inferred type
     squareSum :: forall t. Num [t] => [([t], [t], [t])] -> [t]

Can some one explain me, How to fix this? 有人可以解释一下,该如何解决? What is this error conveying exactly? 这个错误到底传达了什么?

Original Question 原始问题

You posted: 您发布了:

Prelude> let squareSum list = [result | (x, y, z) <- list, result <- x^2 + y^2 + z^2]

<interactive>:3:5:
    Non type-variable argument in the constraint: Num [t]
    (Use FlexibleContexts to permit this)
    When checking that ‘squareSum’ has the inferred type
      squareSum :: forall t. Num [t] => [([t], [t], [t])] -> [t]

This is coming from the inference that is the computation x^2 + y^2 + z^2 must be a list, due to using it as a source of values in a list comprehension ( result <- ... ). 这来自推论,因为计算x^2 + y^2 + z^2必须是列表,因为将其用作列表推导中的值源( result <- ... )。 And if that is a list, then the mathematical operators are over list typed value, which means your initial variables, list must be a list of tuples of lists ( [([t],[t],[t])] ) and each list must in some way be valid numbers ( Num [t] ). 并且如果那是一个列表,则数学运算符将超过列表类型的值,这意味着您的初始变量, list必须是列表元组的列表( [([t],[t],[t])] )和每个列表在某种程度上必须是有效数字( Num [t] )。

Comment Question 评论问题

Prelude> let squareSum list = [ x^2 + y^2 + z^2 | (x, y, z) <- list]
Prelude> squareSum [1,2,3]

<interactive>:9:1:
    Non type-variable argument in the constraint: Num (t, t, t)
    (Use FlexibleContexts to permit this)
    When checking that ‘it’ has the inferred type
      it :: forall t. (Num t, Num (t, t, t)) => [t]

Now you say that the variable list contains tuples ( (x, y, z) <- list ) but then you define list as [1,2,3] . 现在,您说变量list包含元组( (x, y, z) <- list ),但随后将list定义为[1,2,3] To satisfy both, the numeric literals of 1 , 2 and 3 must represent tuples, which is possible if you defined a class instance Num (t, t, t) . 同时满足,的数字文本123必须代表元组,这是可能的,如果你定义一个类的实例Num (t, t, t)

What You Want 你想要什么

Carter already told you the solution, but you didn't apply it to a sensible list. 卡特已经告诉过您解决方案,但是您没有将其应用于明智的选择。 How about we define a solution and give it an explicit type so there is less confusion! 我们如何定义解决方案并为其指定显式类型,以减少混乱呢?

Prelude> :{
Prelude| let squareSum :: [(Int,Int,Int)] -> [Int]
Prelude|     squareSum list = [ x^2 + y^2 + z^2 | (x, y, z) <- list]
Prelude| :}
Prelude> squareSum [(1,2,3), (4,5,6)]
[14,77]

Success! 成功! We provided two tuples and get two Int results, yay! 我们提供了两个元组,并获得了两个Int结果,是的!

Following snippet solved my problem. 以下代码段解决了我的问题。

Prelude> let processData fun list = [y | x <- list, let y = fun x]
Prelude> let sumSquare (x, y, z) = x^2 + y^2 + z^2
Prelude> 
Prelude> processData sumSquare [(1, 2, 3), (4, 5, 6)]
[14,77]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM