简体   繁体   English

Haskell 定义数据类型

[英]Haskell Define Data Type

I trying to create a type class Place which has String Coord [Int] , and a testData to store all the element into a list我试图创建一个类型 class Place ,它具有String Coord [Int]和一个testData来将所有元素存储到一个列表中

data Place =  Place String Coord [Int]
             deriving (Ord,Eq,Show,Read)

data Coord = Cord Double Double
             deriving (Ord ,Eq ,Show ,Read)


testData :: [Place]
testData = [  "London"       Cord(51.5  ,-0.1)   0, 0, 5, 8, 8, 0, 0 
              "Cardiff"      Cord(51.5  ,-3.2)   12, 8, 15, 0, 0, 0, 2
              "Norwich"      Cord(52.6  , 1.3)   0, 6, 5, 0, 0, 0, 3 
              "Birmingham"   Cord(52.5  ,-1.9)   0, 2, 10, 7, 8, 2, 2
              "Liverpool"    Cord(53.4  ,-3.0)   8, 16, 20, 3, 4, 9, 2
              "Hull"         Cord(53.8  ,-0.3)   0, 6, 5, 0, 0, 0, 4
              "Newcastle"    Cord(55.0  ,-1.6)   0, 0, 8, 3, 6, 7, 5
              "Belfast"      Cord(54.6  ,-5.9)   10, 18, 14, 0, 6, 5, 2
              "Glasgow"      Cord(55.9  ,-4.3)   7, 5, 3, 0, 6, 5, 0
              "Plymouth"     Cord(50.4  ,-4.1)   4, 9, 0, 0, 0, 6, 5
              "Aberdeen"     Cord(57.1  ,-2.1)   0, 0, 6, 5, 8, 2, 0
              "Stornoway"    Cord(58.2  ,-6.4)   15, 6, 15, 0, 0, 4, 2
              "Lerwick"      Cord(60.2  ,-1.1)   8, 10, 5, 5, 0, 0, 3
              "St Helier"    Cord(49.2  ,-2.1)     0, 0, 0, 0, 6, 10, 0 ]

But it keep giving me this error但它一直给我这个错误

UP917725.hs:20:15: error:
    • Couldn't match expected type ‘(Double -> Double -> Coord)
                                    -> (Double, Double) -> Integer -> Place’
                  with actual type ‘[Char]’
    • The function ‘"London"’ is applied to three arguments,
      but its type ‘[Char]’ has none
      In the expression: "London" Cord (51.5, - 0.1) 0
      In the expression: ["London" Cord (51.5, - 0.1) 0, 0, 5, 8, ....]
   |
20 | testData = [  "London"       Cord(51.5  ,-0.1)   0, 0, 5, 8, 8, 0, 0     |               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

It keep saying that the actual type is Char , I also tried putting parenthesis with the int list aswell, So I probably think the error is somewhere around the definition of the datatype它一直说实际类型是Char ,我也尝试在 int 列表中加上括号,所以我可能认为错误出在数据类型定义的某个地方

Your datatype definition is perfectly fine, you are just missing the Place data constructor in your list.您的数据类型定义非常好,您只是缺少列表中的Place数据构造函数。 Also function application has a different syntax in Haskell .此外 function 应用程序在Haskell中具有不同的语法。 Instead of Cord(53.8, -0.3) you have to use Cord 53.8 (-0.3) .您必须使用Cord 53.8 (-0.3)而不是Cord(53.8, -0.3) ) 。

For example:例如:

testData :: [Place]
testData = [ Place "London" (Cord 51.5 (-0.1)) [0, 0, 5, 8, 8, 0, 0]
           , Place "Cardiff" (Cord 51.5 (-3.2)) [12, 8, 15, 0, 0, 0, 2]
           -- etc.
           ]

Let's reduce this to the minimal list that has the same problem:让我们将其简化为具有相同问题的最小列表:

testData = [  "London"       Cord(51.5  ,-0.1)   0, 0, 5, 8, 8, 0, 0 
              "Cardiff"      Cord(51.5  ,-3.2)   12, 8, 15, 0, 0, 0, 2 ]

First note that though Haskell does have indentation sensitivity, the line break does not mean anything here.首先请注意,虽然 Haskell 确实具有缩进敏感性,但换行在这里并不意味着什么。 This is actually parsed as这实际上被解析为

testData = [  ("London"       Cord(51.5  ,-0.1)   0), 0, 5, 8, 8, 0, (0 
               "Cardiff"      Cord(51.5  ,-3.2)   12), 8, 15, 0, 0, 0, 2 ]

or或者

testData = [ "London" Cord (51.5,-0.1) 0
           , 0
           ‥‥
           , 0
           , 0 "Cardiff" Cord (51.5,-3.2) 12
           , 8
           ‥‥ 
           , 2 
           ]

ie the separators of list elements are completely different from what you intended.即列表元素的分隔符与您的意图完全不同。 To get that each city is an entry, put the commas between them and and ensure that any finer-lever commas are encapsulated in parens – in doubt, by putting each list entry entirely in a paren:为了让每个城市都是一个条目,请在它们之间放置逗号,并确保任何更精细的逗号都封装在括号中——毫无疑问,将每个列表条目完全放在括号中:

testData = [ ("London"       Cord(51.5  ,-0.1)   0, 0, 5, 8, 8, 0, 0)
           , ("Cardiff"      Cord(51.5  ,-3.2)   12, 8, 15, 0, 0, 0, 2) ]

Now, by simply having commas in parens you make these list entries tuples .现在,只需在括号中使用逗号,您就可以创建这些列表条目tuples That's not what you want, but let's go for it for now...这不是你想要的,但现在让我们 go 吧......

The first tuple entry would be第一个元组条目将是

   "London" Cord(51.5  ,-0.1) 0

Again, whitespace doesn't really affect parsing;同样,空格并不会真正影响解析。 this is the same as这和

   "London" (Cord) (51.5,-0.1) (0)

which would mean, you apply the function "London" to the arguments Coord , then (51.5,-0.1) , then 0 .这意味着,您将 function "London"应用于 arguments Coord ,然后(51.5,-0.1) ,然后0 How can "London" possibly be a function? "London"怎么可能是 function?

No, what you want is have "London" as the first data field of a Place value.不,您想要的是将"London"作为Place值的第一个数据字段 Place is a tuple-ish type, but it's not literally a tuple: to generate a Place value out of the desired data-field values, you need to use its constructor , which also happens to be called Place . Place是一个元组类型,但它并不是真正的元组:要从所需的数据字段值生成Place值,您需要使用它的构造函数,它也恰好称为Place (It's common in Haskell to give constructors the same name as the type they construct, but it could just as well be called different.) So that would be (在 Haskell 中很常见,为构造函数赋予与它们构造的类型相同的名称,但也可以称为不同的名称。)

     Place "London" Cord(51.5  ,-0.1) 0

Still not right: Cord and (51.5,-0.1) are separate here regardless of whether you have whitespace between them, so this would be parsed仍然不对: Cord(51.5,-0.1)在这里是分开的,不管它们之间是否有空格,所以这将被解析

     Place ("London") (Cord) (51.5  ,-0.1) (0)

Instead you want相反,你想要

     Place "London" (Cord (51.5  ,-0.1)) 0

We're almost done.我们快完成了。 Only, that 0 wasn't actually supposed to be provided on its own: what you want there is a list of ints, of which 0 was only the head.只是,实际上不应该单独提供0 :您想要的是一个整数列表,其中0只是头部。

Well, solution is to put all those list-entry numbers in brackets, specifically list-brackets:好吧,解决方案是将所有这些列表条目编号放在括号中,特别是列表括号:

     Place "London" (Cord (51.5  ,-0.1)) [0, 0, 5, 8, 8, 0, 0]

Then, one last thing: Coord is also a constructor, taking floating-point numbers.然后,最后一件事: Coord也是一个构造函数,采用浮点数。 But you give it a single tuple of such numbers.但是你给它一个这样的数字的元组 So instead of Cord (51.5,-0.1) , you want Cord 51.5 (-0.1) .因此,您需要Cord 51.5 (-0.1)而不是Cord (51.5,-0.1) ) 。

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

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