如何在列表列表上获得递归功能的输出,如何获得多个列表?

发布于 2025-01-22 20:00:29 字数 604 浏览 0 评论 0原文

我正在尝试使用一个递归功能,该功能打印所有具有最大长度列表中最大长度的列表(如果它们的长度相同,则可能是一个或多个)

给定输入,例如:

[[],[3],[2],[6],[3,6],[2,6],[4],[3,4],[2,4],[5],[3,5],[2,5],[4,5],[3,4,5],[2,4,5],[1]]

输出应包含两个最长列表

[3,4,5],[2,4,5]

:以下功能仅打印第一个列表:[3,4,5]

  longest :: Ord a => [[a]] -> [a]
  longest [y] = y    --base case: if there's only one element left, return it.
  longest (x:y:lst) --extract the first two elements x, y from the list.  
     | length x < length y = longest (y:lst)
     | otherwise  = longest (x:lst)

注意:我必须使用恢复

I am trying to use a recursive function that prints all lists that has the maximum length out of the lists ( could be one or more if they have the same length)

given an input such as :

[[],[3],[2],[6],[3,6],[2,6],[4],[3,4],[2,4],[5],[3,5],[2,5],[4,5],[3,4,5],[2,4,5],[1]]

the output should contain both the longest lists :

[3,4,5],[2,4,5]

My following function prints only the first list: [3,4,5]

  longest :: Ord a => [[a]] -> [a]
  longest [y] = y    --base case: if there's only one element left, return it.
  longest (x:y:lst) --extract the first two elements x, y from the list.  
     | length x < length y = longest (y:lst)
     | otherwise  = longest (x:lst)

Note: I must used recuersion

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

别想她 2025-01-29 20:00:29

您可以使用累加器跟踪迄今为止获得的最长项目以及此列表的列表,因此:

longest :: [[a]] -> [[a]]
longest (x:xs) = go xs (length x) [x]
    go [] _ ys = reverse ys
    go (x:xs) n ys
        | n' > n = go xs n' [x]
        | n' == n = go xs n (x:ys)
        | otherwise = go xs n ys
        where n' = length x

You can use accumulators to keep track of the thus far obtained longest item and the list of lists for this, so:

longest :: [[a]] -> [[a]]
longest (x:xs) = go xs (length x) [x]
    go [] _ ys = reverse ys
    go (x:xs) n ys
        | n' > n = go xs n' [x]
        | n' == n = go xs n (x:ys)
        | otherwise = go xs n ys
        where n' = length x
十二 2025-01-29 20:00:29

我发现您的方法很复杂,因为您将在参数开始时积累结果,并且有必要进一步使用。
考虑此解决方案,该解决方案将未来结果累积到第二个辅助参数中。

longest :: [[a]] -> [[a]]
longest lst = longest' lst [[]]

longest' :: [[a]]->[[a]]->[[a]] -- input, working state, output
longest' [] x = x --base case: if there's empty input return working state.
longest' (y:lst) x
 | length (head x) < length y = longest' lst [y]
 | length (head x) == length y = longest' lst (x++[y])
 | otherwise = longest' lst x

inp = [[],[3],[2],[6],[3,6],[2,6],[4],[3,4],[2,4],[5],[3,5],[2,5],[4,5],[3,4,5],[2,4,5],[1]]

main = putStrLn $ show $ longest inp

输出:

[[3,4,5],[2,4,5]]

您可以在此设计中的SO或标准库中在Haskell中看到这种方法:

longest lst = longest' lst [[]]
  where 
    longest' [] x = x --base case: if there's empty input return helper.
    longest' (y:lst) x
     | length (head x) < length y = longest' lst [y]
     | length (head x) == length y = longest' lst (x++[y])
     | otherwise = longest' lst x

I find your approach complicated in that you will accumulate the result at the beginning of the parameter, and it is necessary to work with it further.
Consider this solution, which accumulates the future result into a second auxiliary parameter.

longest :: [[a]] -> [[a]]
longest lst = longest' lst [[]]

longest' :: [[a]]->[[a]]->[[a]] -- input, working state, output
longest' [] x = x --base case: if there's empty input return working state.
longest' (y:lst) x
 | length (head x) < length y = longest' lst [y]
 | length (head x) == length y = longest' lst (x++[y])
 | otherwise = longest' lst x

inp = [[],[3],[2],[6],[3,6],[2,6],[4],[3,4],[2,4],[5],[3,5],[2,5],[4,5],[3,4,5],[2,4,5],[1]]

main = putStrLn $ show $ longest inp

Output:

[[3,4,5],[2,4,5]]

This approach you can see in Haskell on the SO or in the standard libraries in this design:

longest lst = longest' lst [[]]
  where 
    longest' [] x = x --base case: if there's empty input return helper.
    longest' (y:lst) x
     | length (head x) < length y = longest' lst [y]
     | length (head x) == length y = longest' lst (x++[y])
     | otherwise = longest' lst x
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文