简体   繁体   English

我保留错误“解析输入 ins 时出错”

[英]I keeping the error "error on parsing on input ins"

module HW2sol 
where import HW2types

ins :: Eq a => a -> Bag a -> Bag a
ins x [] = [(x,1)]
ins x (y:ys) =
    if x == fst y
        then (x, snd y + 1) : ys
        else y : (ins x ys)

HMW2types in this code is just a file that contains all of the declarations for the bag type.此代码中的 HMW2types 只是一个包含包类型的所有声明的文件。 I have even copied and pasted other functions from online and they get the same error.我什至从网上复制并粘贴了其他功能,但他们得到了同样的错误。 This function takes an element and adds it to a bag.这个 function 获取一个元素并将其添加到包中。

[2 of 2] Compiling HW2sol ( HMW2sol.hs, HMW2sol.o ) [2 of 2] 编译 HW2sol ( HMW2sol.hs, HMW2sol.o )

HMW2sol.hs:5:1: parse error on input `ins' HMW2sol.hs:5:1:输入“ins”时出现解析错误

You may be aware of Haskell's indentation rules.您可能知道 Haskell 的缩进规则。 Anywhere you have a block of declarations (as in a let <decls> in <expr> , or a where , etc) or a block of statements (as in a do block), or a block of patterns & branches (as in a case expression) Haskell identifies the start of each entry in the block by the following logic:任何地方你有一个声明块(如let <decls> in <expr>where等)或语句块(如do块中),或模式和分支块(如case表达式)Haskell 通过以下逻辑标识块中每个条目的开始:

  1. Identify the column of the first character of the first entry in the block.标识块中第一个条目的第一个字符的列。 Call this column C .将此列称为 C
  2. The next line that is indented less than C indicates the end of the block (and is not part of the block).缩进小于C的下一行表示块的结尾(并且不是块的一部分)。
  3. Before the end of the block, any line that is indented exactly C characters starts a new entry in the block.在块结束之前,任何精确缩进C 个字符的行都会在块中开始一个新条目。
  4. Before the end of the block, any line that is indented more than C is a continuation line of the previous entry在块结束之前,任何缩进超过C的行都是前一个条目的续行

This logic is consistently applied to all of Haskell's "block" constructs.这个逻辑始终适用于 Haskell 的所有“块”结构。 They all have to be aligned this way 1 .它们都必须以这种方式对齐1 And it turns out that the top-level declarations in a module form an aligned block ;事实证明,模块中的顶级声明形成了一个对齐的块 We just don't usually bother to think of them that way because they are conventionally started at column 1 (which means it isn't possible to end the block as no line can start before the first column. people normally just put all their top-level declarations at the start of the line and indent any continuations without ever thinking about "aligning the module's main block").我们只是通常不会费心去想它们,因为它们通常从第 1 列开始(这意味着不可能结束块,因为在第一列之前没有行可以开始。人们通常只是把他们所有的顶部-level 声明在行的开头并缩进任何延续而无需考虑“对齐模块的主块”)。

However, your code does not (successfully) use this conventional layout.但是,您的代码没有(成功地)使用这种传统布局。 Because you didn't insert a linebreak after the where , the first declaration begins at column 7, not column 1!因为您没有在where之后插入换行符,所以第一个声明从第 7 列开始,而不是第 1 列!

where import HW2types
      ^
1234567

Your type declaration of ins begins on column 1, so by rule 2 above (1 is less than 7) this indicates the end of the module's block of definitions;您的ins类型声明从第 1 列开始,因此根据上面的规则 2(1 小于 7),这表示模块定义块的结尾; the compiler has to parse ins:: Eq a =>... as something that can follow the main block in the module, instead of parsing it as a declaration in the main block.编译器必须将ins:: Eq a =>...解析为可以在模块中的主块之后的东西,而不是将其解析为主块的声明。 That's why you get a parse error (nothing can follow the main block).这就是为什么你会得到一个解析错误(没有任何东西可以跟随主块)。

If you start your module's main block at column 7 then all of your declarations have to be indented to column 7. This actually works:如果您在第 7 列开始模块的主块,那么您的所有声明都必须缩进到第 7 列。这实际上有效:

module HW2sol 
where import HW2types

      ins :: Eq a => a -> Bag a -> Bag a
      ins x [] = [(x,1)]
      ins x (y:ys) =
          if x == fst y
              then (x, snd y + 1) : ys
              else y : (ins x ys)

However you'll probably find it much easier to simply put a line break after the where and have import HW2types on a new line.但是,您可能会发现在where之后简单地换行并在新行上import HW2types会容易得多。


1 Alternatively, blocks and their entries can be explicitly delimited with braces and semicolons. 1或者,块及其条目可以用大括号和分号显式分隔。 But this is not usual style in Haskell.但这在 Haskell 中不是通常的风格。

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

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