为什么解析错误?缩进? [英] Why parse error? Indentation?

查看:195
本文介绍了为什么解析错误?缩进?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了这段代码:

addNums key num = add [] key num
其中添加res a:作为b:bs
| a == [] = res
|否则=添加res:(a + b)作为bs

在第3行解释器说: p>


解析错误(可能是不正确的缩进)

我无法找到错误,无论是代码还是缩进。我为每个选项卡放置了四个空格。

注解:
$ b

即使这样也不能编译:

  addNums key num = add [] key num 
where add res a:as b:bs
| a == [] = res
|第2行: $ b







$
$ b


模式中的解析错误:add


解决方案 div>

Haskell中的主要的indendation规则是,如果你想在另一行继续定义,它必须比你定义的东西更加缩进。在这种情况下,对于 add 函数的警卫 less 缩进,这就是编译器所抱怨的。



忽略代码中的其他错误,缩进应该是这样的:

  addNums key num = add [] key num 
where add a res:as b:bs
| a == [] = res
|否则= add res:(a + b)as bs

另请注意,缩进的确切数量无关紧要,只有继续行相对于被定义的东西的缩进。



你的代码的另一个语法问题是你似乎误解了Haskell的优先规则。在Haskell中,函数应用比任何运算符都更紧密,所以 add res a:as b:bs 被解析为(添加res a):(如b):bs ,而你的意思是 add res(a:as)(b:bs) p>

最后的问题是类型错误。 (:)运算符的类型为 a - > [a] - > [a] ,这意味着它需要元素列表,并生成列表。在你的代码 res:(a + b)中,你似乎有相反的结果,因为 res 是一个列表, a + b 是元素。由于Haskell中没有运算符将单个元素附加到列表的 end ,所以必须使用列表级联运算符(++):: [a ] - > [a] - > [a] 改为: res ++ [a + b]



您还将元素 a 列表进行比较 [] (a:as)不匹配。最简单的解决方案是添加另一个模式,而不是警惕。



把所有这些放在一起,这段代码应该可以做你想做的事:

  addNums key num = add [] key num 
where add res [] _ = res
add res(a:as )(b:bs)= add(res ++ [a + b])为bs

PS反复追加到列表的末尾并不是非常有效。事实上,它是O(n 2 )。您可能希望添加到前面,并在完成后颠倒列表。这是O(n)。






参考资料:


I wrote this code:

addNums key num = add [] key num
    where add res a:as b:bs
        | a == [] = res
        | otherwise = add res:(a+b) as bs

At line 3 the interpreter says:

parse error (possibly incorrect indentation)

I could not find something wrong, neither with the code nor with the indentation. I put four spaces for each tab.

Annotation:

Even this does not compile:

addNums key num = add [] key num
    where add res a:as b:bs
            | a == [] = res
            | otherwise = add res:(a+b) as bs

Line 2:

Parse error in pattern: add

解决方案

The main indendation rule in Haskell is that if you want to continue a definition on another line, it has to be further indented than the the thing you're defining. In this case the guards for your add function are less indented, so that's what the compiler is complaining about.

Disregarding the other errors in your code, the indentation should be something like this:

addNums key num = add [] key num
    where add res a:as b:bs
            | a == [] = res
            | otherwise = add res:(a+b) as bs

Also note that the exact amount of indentation does not matter, only the indentation of the continuing lines relative to the thing being defined.

Another syntactic problem with your code is that you seem to have misunderstood the precedence rules of Haskell. In Haskell, function application binds tighter than any operator, so add res a:as b:bs is parsed as (add res a):(as b):bs, while you meant add res (a:as) (b:bs).

The final problems are type errors. The (:) operator has the type a -> [a] -> [a], which means that it takes an element and a list, and produces a list. In your code res:(a+b), you appear to have this reversed, as res is a list and a+b is the element. Since there is no operator in Haskell to append a single element to the end of a list, you'll have to use the list concatenation operator (++) :: [a] -> [a] -> [a] instead: res ++ [a+b].

You're also comparing the element a to the list [] in your guard. This is probably not what you meant, and the pattern (a:as) would not match if the list was empty. The easiest solution to this is to add another pattern instead of your guard.

Putting all of this together, this code should hopefully do what you intended:

addNums key num = add [] key num
    where add res [] _ = res
          add res (a:as) (b:bs) = add (res ++ [a+b]) as bs

P.S. Repeatedly appending to the end of a list is not very efficient. In fact, it's O(n2). You might want to add to the front instead and reverse the list when you're done. This is O(n).


References:

这篇关于为什么解析错误?缩进?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆