过滤我自己类型的列表 - 元组?

发布于 2024-11-06 03:35:06 字数 177 浏览 0 评论 0原文

如何通过元组中的第三项过滤此类型的列表:

type Car = (String, [String], Int [String])

我看到了 snd 和 fst 方法,但在这里我认为这不起作用,我不确定如何在不使用 '_' 通配符的情况下进行映射。

How can I filter a list of this type by the third item in the tuple:

type Car = (String, [String], Int [String])

I saw the snd and fst methods but here i dont think this will work and im not sure how to map without using the '_' wildcard.

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

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

发布评论

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

评论(3

只为一人 2024-11-13 03:35:06

对于具有两个以上元素的元组,没有任何预定义函数,例如 fstsnd。正如您所说,您可以使用模式匹配和通配符 _ 来完成这项工作。

 cars = [ ("Foo", ["x", "y"], 2009, ["ab", "cd"]
        , ("Bar", ["z"],      1997, [])
        ]

 newCars = filter condition cars
     where condition (_, _, n, _) = n > 2005

但是,这通常表明您应该从使用元组更改为使用记录类型。

 data Car = Car { model :: String
                , foo   :: [String]
                , year  :: Int
                , bar   :: [String] 
                }

 cars = [ Car "Foo" ["x", "y"] 2009 ["ab", "cd"]
        , Car "Bar" ["z"]      1997 []
        ]

现在,您可以像使用 fstmodelfooyearbar元组上的 code> 和 snd 。

 newCars = filter ((> 2005) . year) cars

There aren't any pre-defined functions like fst or snd for tuples with more than two elements. As you said, you can use pattern matching and the wild card _ to do the job.

 cars = [ ("Foo", ["x", "y"], 2009, ["ab", "cd"]
        , ("Bar", ["z"],      1997, [])
        ]

 newCars = filter condition cars
     where condition (_, _, n, _) = n > 2005

However, this is usually a sign that you should change from using tuples to a record type.

 data Car = Car { model :: String
                , foo   :: [String]
                , year  :: Int
                , bar   :: [String] 
                }

 cars = [ Car "Foo" ["x", "y"] 2009 ["ab", "cd"]
        , Car "Bar" ["z"]      1997 []
        ]

Now, you can use model, foo, year and bar like you would use fst and snd on tuples.

 newCars = filter ((> 2005) . year) cars
别挽留 2024-11-13 03:35:06

或者您可以使用 Data.Tuple.Utils

MissingH 也充满了其他好东西;几乎我所有的项目都在某个地方或其他地方使用它。

Or you could just use Data.Tuple.Utils?

MissingH is full of other good stuff too; almost all of my projects use it somewhere or other.

梦里南柯 2024-11-13 03:35:06

这是我对类似问题的解决方案:

  --construct a list of stock records with the number in stock less than the reorder level.

  getstock (name, stock, reorder) = stock
  getreorder (name, stock, reorder) = reorder

  check l = filter (\x ->(getstock x < getreorder x)) l

  main = print(check [("RAM",9,10),("ROM",12,10),("PROM",20,21)]); --[("RAM",9,10),("PROM",20,21)] 

关键是要理解过滤函数采用谓词,而不是布尔值。

因此,简单地说

filter(getstock < getreorder)

是行不通的,

`filter (getstock < 10)

则有效

Here is my solution for a similar problem:

  --construct a list of stock records with the number in stock less than the reorder level.

  getstock (name, stock, reorder) = stock
  getreorder (name, stock, reorder) = reorder

  check l = filter (\x ->(getstock x < getreorder x)) l

  main = print(check [("RAM",9,10),("ROM",12,10),("PROM",20,21)]); --[("RAM",9,10),("PROM",20,21)] 

The key was to understand that the filter function takes a predicate, not a boolean value.

So, simply saying

filter(getstock < getreorder)

would not work,

while

`filter (getstock < 10)

would

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