为什么这个 Haskell 不正确?

发布于 2024-08-12 00:59:00 字数 1128 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

与酒说心事 2024-08-19 00:59:00

你的代码不正确。

longest::[Integer]->[Integer]->[Integer]
max a b = if length a > length b then a else b

应该是

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

你需要一个 main 函数

main = do print llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

只是为了改进你的代码,如果你有一个函数签名并给它一个小写字母作为它的类型(比如字母 a),它就会变得通用。例如,

longest:: [a] -> [a] -> [a]
longest x y = if length x > length y then x else y

这意味着它不仅仅可以处理整数列表,还可以处理任何内容的列表。突然间你就拥有了一个非常可重用的函数。

Your code isn't correct.

This

longest::[Integer]->[Integer]->[Integer]
max a b = if length a > length b then a else b

should be

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

And you need a main function

main = do print llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

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

longest:: [a] -> [a] -> [a]
longest x y = if length x > length y then x else y

Means that rather than just working on lists of Integers, it works on lists of anything. Suddenly you have a very reusable function.

不乱于心 2024-08-19 00:59:00

在该行中,

llfs li = length(foldl longest group(li))

解释器将 group 视为 foldl 的第二个参数。编写 group(li) 与编写 group li 没有什么不同。

另外,foldl 需要一个初始值。另一方面,foldl1 使用第一个列表元素作为其初始值。尝试:(

llfs li = length (foldl1 longest (group li))

编辑以删除第一个错误答案。)

In the line

llfs li = length(foldl longest group(li))

the interpreter is treating group as the second argument to foldl. Writing group(li) is no different from writing group li.

Also, foldl needs an initial value. foldl1, on the other hand, uses the first list element for its initial value. Try:

llfs li = length (foldl1 longest (group li))

(Edited to remove the first, wrong answer.)

潜移默化 2024-08-19 00:59:00
module Main where

import Data.List

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

llfs::[Integer]->Int
llfs li = length $ foldl1 longest $ group li

main = do
    putStrLn $ show $ llfs [1, 2, 3, 3, 4, 5, 1, 1, 1]
module Main where

import Data.List

longest::[Integer]->[Integer]->[Integer]
longest a b = if length a > length b then a else b

llfs::[Integer]->Int
llfs li = length $ foldl1 longest $ group li

main = do
    putStrLn $ show $ llfs [1, 2, 3, 3, 4, 5, 1, 1, 1]
一个人的旅程 2024-08-19 00:59:00

问题是最后一行没有定义函数,正如其他人所说。您的代码中还有更多错误。看来这就是您想要做的:

import Data.List

longest_group_size :: [Integer] -> Int
longest_group_size = maximum . map length . group

main :: IO ()
main = print $ longest_group_size [1, 2, 3, 3, 4, 5, 1, 1, 1]

观察:

  1. 您需要 import Data.List 以便使用
  2. 无需使用 foldr 在这种情况下:通过使用 map每组的长度只计算一次。
  3. 当然,这确实意味着我们需要调用另一个函数的帮助,最大值

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:

import Data.List

longest_group_size :: [Integer] -> Int
longest_group_size = maximum . map length . group

main :: IO ()
main = print $ longest_group_size [1, 2, 3, 3, 4, 5, 1, 1, 1]

Observe that:

  1. You need to import Data.List in order to use group.
  2. No need to use foldr in this case: by using map the length of each group is only calculated once.
  3. This does mean, of course, that we call in the help of another function, maximum.
如梦亦如幻 2024-08-19 00:59:00

您无法像在 python 或其他脚本语言中那样在文件范围内调用函数。因此,最后一行中对 llfs 的“调用”是一个错误。尝试在 main 中打印结果:

main = print (llfs [1, 2, 3, 3, 4, 5, 1, 1, 1])

此时“函数调用”看起来像是一个不完整的函数定义,缺少右侧,这导致了令人惊讶的错误消息:

llfs (...) = abc

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 in main:

main = print (llfs [1, 2, 3, 3, 4, 5, 1, 1, 1])

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:

llfs (...) = abc
陌若浮生 2024-08-19 00:59:00

问题是这一行:

llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

That's not a function statements.我认为您正在尝试进行函数调用,在这种情况下您需要将其放在 main 声明中。您还可以将 Haskell 文件加载到解释器中(例如,ghci)并在解释器控制台中执行函数调用。

The problem is this line:

llfs([1, 2, 3, 3, 4, 5, 1, 1, 1])

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.

大姐,你呐 2024-08-19 00:59:00

这不是这两个错误的直接原因,但我认为这是造成您误解的一个因素。在 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 to group 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.

月牙弯弯 2024-08-19 00:59:00

更新的两个小问题。首先,您似乎正在尝试将 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 to foldl. The right way to say that is (group li) rather than group(li) The second is that foldl needs a base case. Caleb's suggestion to use foldl1 is one option that will probably work for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文