简体   繁体   English

Haskell对什么不满意? 抱怨我的类型签名

[英]What is Haskell not happy about? It's complaining about my type signature

I've another issue with my Haskell. 我的Haskell还有另一个问题。 I'm given the following data constructor from a problem, 我从一个问题得到以下数据构造函数,

type Point = (Int, Int)

data Points = Lines Int Int
            | Columns Int Int
            | Union Points Points
            | Intersection Points Points

It's about points on a grid starting (0,0) and (x,y) has x as the horizontal distance from the origin and y as the vertical distance from the origin. 它是关于以(0,0)和(x,y)开头的网格上的点,其中x是到原点的水平距离,y是到原点的垂直距离。

I tried to define a function "Lines" from this, which, given Lines xy would evaluate all points with vertical distance x ~ y on the grid. 我试图从中定义一个函数“ Lines”,给定Lines xy可以计算网格上垂直距离为x〜y的所有点。 eg 例如

> Lines 2 4 
(0,2)(1,2)(2,2)(3,2)....
(0,3)(1,3)(2,3)(3,3)....
(0,4)(1,4)(2,4)(3,4)....

and so on. 等等。 Well what I did, was, 我做的是

Lines :: Int -> Int -> Points
Lines lo hi = [ (_, y) | lo <= y && y <= hi ]

But Haskell complains that; 但是Haskell抱怨说。

Invalid type signature Lines :: Int -> Int -> Points. 类型签名无效行::: Int-> Int->点。
Should be of the form :: 格式应为::

what's this mean? 这是什么意思 "Points" is defined above already...surely "Int" "Points" are regarded as "types"? 上面已经定义了“点” ...肯定是“整数”“点”被视为“类型”吗? I don't see the problem, why is Haskell confused? 我没有看到问题,为什么Haskell感到困惑?

  1. Functions must not start with a capital letter. 函数不能以大写字母开头。 So you need to use lines , not Lines . 因此,您需要使用lines ,而不是Lines This is probably the source of the error message you're seeing. 这可能是您看到的错误消息的来源。

  2. The syntax [ ... ] is for creating a list of results, but your type signature claims that the function returns Points , which isn't any kind of list. 语法[ ... ]用于创建结果列表 ,但是您的类型签名声称该函数返回Points ,而不是任何类型的列表。 If you meant to return a list of Point values, that's the [Point] type. 如果要返回Point值列表, Point [Point]类型。

  3. I have literally no idea what your implementation of Lines is even trying to do. 我真的不知道您对Lines的实现甚至试图做什么。 The syntax makes no sense to me. 语法对我来说毫无意义。


OK, so taking your comments into account... 好的,因此请考虑您的评论...

  • You can generate a list of numbers between lo and hi by writing [lo .. hi] . 您可以通过编写[lo .. hi]来生成lohi之间的数字列表。

  • You say an "arbitrary" value can go in X, but you need to pin down exactly what that means. 您说“任意”值可以用X表示,但是您需要确切说明这是什么意思。 Your example seems to suggest you want the numbers from 0 upwards, forever. 您的示例似乎表明您希望永远将数字从0向上。 The way to generate that list is [0 .. ] . 生成该列表的方法是[0 .. ] (Not giving an upper limit makes the list endless.) (不给出上限会使列表无休止。)

  • Your example suggests you want a list of lists, with the inner list containing all points with the same Y-coordinate paired with all possible X-coordinates. 您的示例建议您要一个列表列表,内部列表包含具有相同Y坐标和所有可能的X坐标的所有点。

So here is one possible way to do that: 因此,这是一种可行的方法:

type Point = (Int, Int)

lines :: Int -> Int -> [[Point]]
lines lo hi = [ [(x,y) | x <- [0..]] | y <- [lo .. hi] ]

That's perhaps a teeny bit hard to read, with all those opening and closing brackets, so perhaps I can make it slightly cleaner: 加上所有的左右括号,这可能有点难以理解,所以也许我可以使其更简洁一些:

lines lo hi =
  let
    xs = [0..]
    ys = [lo .. hi]
  in [    [(x,y) | x <- xs]    | y <- ys]

If you run this, you get 如果运行此命令,您将得到

> lines 2 4
[[(0,2), (1,2), (2,2), ...],
[(0,3), (1,3), (2,3), ...],
[(0,4), (1,4), (2,4), ...]]

In other words, the outer list has 3 elements (Y=2, Y=3 and Y=4), and each of the three inner lists is infinitely long (every possible positive X value). 换句话说,外部列表具有3个元素(Y = 2,Y = 3和Y = 4),并且三个内部列表中的每一个都无限长(每个可能的正X值)。

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

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