找到给定条件的列表的最后一个元素

发布于 2025-02-01 03:00:08 字数 575 浏览 1 评论 0原文

我正在尝试编写一个函数,该函数鉴于所有ints都大于100

isLeast100All :: [Int] -> [Int]
isLeast100All list = filter (>100) list

lastList :: [Integer] -> Integer
lastList list' = case list' of
  isLeast100All list
    | list == []   -> 0
    | list == [x]  -> x
    | list == [x:xs] -> lastList xs

。 isleast100all“

我觉得这里很简单,但我不确定它是什么。我正在根据以下定义来基于我的最后列表函数:

lastList :: [Integer] -> Integer
lastList x = case x of
  [] -> 0
  [x] -> x
  x:xs -> lastList xs

I'm trying to write a function that finds the last element of a list of Ints given that all the Ints are greater than 100. This is the code I have written so far:

isLeast100All :: [Int] -> [Int]
isLeast100All list = filter (>100) list

lastList :: [Integer] -> Integer
lastList list' = case list' of
  isLeast100All list
    | list == []   -> 0
    | list == [x]  -> x
    | list == [x:xs] -> lastList xs

which gives me the error: "Parse error in pattern: isLeast100All"

I feel like there's something simple that I'm missing here but I'm not sure what it is. I'm basing my lastList function off the following definition:

lastList :: [Integer] -> Integer
lastList x = case x of
  [] -> 0
  [x] -> x
  x:xs -> lastList xs

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

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

发布评论

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

评论(2

软糖 2025-02-08 03:00:08

您需要修复您的类型并使用过滤而不是MAP iSleast100All:或仅:

isLeast100All :: [Integer] -> [Integer]
isLeast100All = filter (> 100)

lastList :: [Integer] -> Integer
lastList list' = case isLeast100All list' of
    []   -> 0 
    xs  -> last xs

或仅:

lastList :: [Integer] -> Integer
lastList l = last $ 0 : filter (> 100) l

You need to fix your types and use filter instead of map for isLeast100All:

isLeast100All :: [Integer] -> [Integer]
isLeast100All = filter (> 100)

lastList :: [Integer] -> Integer
lastList list' = case isLeast100All list' of
    []   -> 0 
    xs  -> last xs

Or just:

lastList :: [Integer] -> Integer
lastList l = last $ 0 : filter (> 100) l
白况 2025-02-08 03:00:08

您对lastList的定义有两个问题。您已经混淆了list列表', [x:xs] 应该是(x:xs),并且您正在混合谓词后卫和表达的案例。

这是您可以使用谓词后卫来定义它的一种方法:

lastList :: [Integer] -> Integer
lastList list' = myLast $ isLeast100All list'

myLast :: [Integer] -> Integer
myLast list
    | list == [] = 0
    | tail list == [] = head list
    | otherwise = myLast $ tail list

是另一种使用情况...

lastList :: [Integer] -> Integer
astList list' = case isLeast100All list' of
    [] -> 0
    [x] -> x
    (x:xs) -> lastList xs

这 但是我的目标是使它们与您的代码尽可能相似。

Guru Stron的答案提供了更简单的选择。

There are a couple of problems with your definition of lastList. You have confused list and list', [x:xs] should be (x:xs), and you're mixing predicate guards and case ... of expression.

Here's one way you can define it using predicate guards:

lastList :: [Integer] -> Integer
lastList list' = myLast $ isLeast100All list'

myLast :: [Integer] -> Integer
myLast list
    | list == [] = 0
    | tail list == [] = head list
    | otherwise = myLast $ tail list

Here's another using case ... of:

lastList :: [Integer] -> Integer
astList list' = case isLeast100All list' of
    [] -> 0
    [x] -> x
    (x:xs) -> lastList xs

This latter one is needlessly inefficient because the filtering function isLeast100All is applied to the entire remaining list at every level of recursion but I aimed to make these as similar to your code as possible.

The answer by Guru Stron gives simpler alternatives.

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