为什么这个 Haskell 不正确?
我有一个 Haskell 文件,如下所示:
longest::[Integer]->[Integer]->[Integer]
max a b = if length a > length b then a else b
llfs::[Integer]->Integer
llfs li = length(foldl longest group(li))
llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])
应该在最后打印出函数调用的结果,但是当我运行该文件时,我收到此错误:
parse error (possibly incorrect indentation)
我不明白我做错了什么。我应该做什么来修复它?
编辑
将最后一行放入主函数后,如下所示:
import List
longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b
llfs::[Integer]->Integer
llfs li = length(foldl longest group(li))
main = print (llfs [1, 2, 3, 3, 4, 5, 1, 1, 1])
我现在收到以下错误:
C:\Users\Martin\Desktop\Haskell\Q1.hs:7:31:
Couldn't match expected type `[Integer]'
against inferred type `[a] -> [[a]]'
In the second argument of `foldl', namely `group'
In the first argument of `length', namely
`(foldl longest group (li))'
In the expression: length (foldl longest group (li))
这个看起来有点困难!我该如何解决?
I have a Haskell file which looks like this:
longest::[Integer]->[Integer]->[Integer]
max a b = if length a > length b then a else b
llfs::[Integer]->Integer
llfs li = length(foldl longest group(li))
llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])
Which should print out the result of the function call at the end, however when I run the file I get this error:
parse error (possibly incorrect indentation)
I don't understand what I'm doing wrong. What should I do to fix it?
Edit
After putting the last line inside the main function, like this:
import List
longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b
llfs::[Integer]->Integer
llfs li = length(foldl longest group(li))
main = print (llfs [1, 2, 3, 3, 4, 5, 1, 1, 1])
I now get the following error:
C:\Users\Martin\Desktop\Haskell\Q1.hs:7:31:
Couldn't match expected type `[Integer]'
against inferred type `[a] -> [[a]]'
In the second argument of `foldl', namely `group'
In the first argument of `length', namely
`(foldl longest group (li))'
In the expression: length (foldl longest group (li))
This one looks a little more difficult! How do I solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
你的代码不正确。
这
应该是
你需要一个 main 函数
只是为了改进你的代码,如果你有一个函数签名并给它一个小写字母作为它的类型(比如字母 a),它就会变得通用。例如,
这意味着它不仅仅可以处理整数列表,还可以处理任何内容的列表。突然间你就拥有了一个非常可重用的函数。
Your code isn't correct.
This
should be
And you need a main function
Just for the sake of improving your code, if you have a function signature and give it a lower case letter(s) as its type (say the letter a), it becomes generic. For example
Means that rather than just working on lists of Integers, it works on lists of anything. Suddenly you have a very reusable function.
在该行中,
解释器将
group
视为foldl
的第二个参数。编写group(li)
与编写group li
没有什么不同。另外,foldl 需要一个初始值。另一方面,foldl1 使用第一个列表元素作为其初始值。尝试:(
编辑以删除第一个错误答案。)
In the line
the interpreter is treating
group
as the second argument tofoldl
. Writinggroup(li)
is no different from writinggroup li
.Also,
foldl
needs an initial value.foldl1
, on the other hand, uses the first list element for its initial value. Try:(Edited to remove the first, wrong answer.)
问题是最后一行没有定义函数,正如其他人所说。您的代码中还有更多错误。看来这就是您想要做的:
观察:
import
Data.List
以便使用组
。foldr
在这种情况下:通过使用map
每组的长度只计算一次。最大值
。The problem was that the last line did not define a function, as others have stated. More things are wrong in your code. It appears this is what you want to do:
Observe that:
import
Data.List
in order to usegroup
.foldr
in this case: by usingmap
the length of each group is only calculated once.maximum
.您无法像在 python 或其他脚本语言中那样在文件范围内调用函数。因此,最后一行中对
llfs
的“调用”是一个错误。尝试在main
中打印结果:此时“函数调用”看起来像是一个不完整的函数定义,缺少右侧,这导致了令人惊讶的错误消息:
You cannot call a function at file scope like you would do in python or other scripting languages. Therefore the "call" to
llfs
in the last line is an error. Try printing the result inmain
:At the moment the "function call" looks like an incomplete function definition, where the right side is missing, which leads to the surprising error message:
问题是这一行:
That's not a function statements.我认为您正在尝试进行函数调用,在这种情况下您需要将其放在
main
声明中。您还可以将 Haskell 文件加载到解释器中(例如,ghci
)并在解释器控制台中执行函数调用。The problem is this line:
That's not a function declaration. I think you're trying to make a function call, in which case you need to put it inside a
main
declaration. You can also load the Haskell file into an interpreter (e.g.,ghci
) and execute the function call in the interpreter console.这不是这两个错误的直接原因,但我认为这是造成您误解的一个因素。在 Haskell 中,你永远不会写
group(li)
。将单个参数括起来是没有意义的——它完全等同于group li
。如果您尝试将此函数调用的结果传递给另一个函数,则需要将整个表达式括起来 -(group li)
- 或使用$
运算符,例如迦勒提议道。This isn't the direct cause of either error, but I think it's a contributing factor to your misunderstanding. In Haskell, you would never write
group(li)
. Parenthesizing a single argument is pointless — it's exactly equivalent togroup li
. If you're trying to pass the result of this function call to another function, you need to parenthesize the whole expression —(group li)
— or use the$
operator like Caleb suggested.更新的两个小问题。首先,您似乎正在尝试将
group
结果作为参数传递给foldl
。正确的说法是(group li)
而不是group(li)
第二个是foldl
需要一个基本情况。 Caleb 建议使用foldl1
是一个可能适合您的选项。Two small issues with the update. First, it looks like you're trying to pass the
group
result as an argument tofoldl
. The right way to say that is(group li)
rather thangroup(li)
The second is thatfoldl
needs a base case. Caleb's suggestion to usefoldl1
is one option that will probably work for you.