haskell - 如何从haskell中的元组的所有第二个元素中创建一个列表
问题描述
我有一个元组列表,例如 [(1,"A"),(2,"B"),(3,"C")]
现在我想创建一个包含这些元组的所有第二个元素的列表,所以列表应该是 ["A","B","C"]
任何人都可以帮助解决这个问题吗?
解决方案
You can do this by running map snd [(1,"A"),(2,"B"),(3,"C")]
. The map
function is defined to apply the function given as its second argument to each element of the list given as its third argument; the snd
function gets the second element of a tuple.
Alternatively, if you prefer list comprehensions, you could do [x | (_, x) <- [(1,"A"),(2,"B"),(3,"C")]]
; this takes each element of the list, matches it to the pattern (_, x)
(which assigns the second element of each tuple to x
), and then returns each x
.
推荐阅读
- hana - SAP HANA 序列由 Max 或 Max +1 重置
- download - 如何从 openstreetmap.org 下载特定区域中的所有道路和节点
- python - 在 keras 中包含 tensorflow 层的错误
- android - Android Edit text onclicklistener 在第一次点击时不起作用
- java - How to upload video in android using volley
- coldfusion - 我可以将文件写入 cfscript 中指定文件权限的 linux 目录吗?
- spring - aws sns 到 sqs,消息未转换
- ios - 当我从 Swift 调用 Objective-c 函数时,这些函数中的 NSLog 什么都不做
- kubernetes - $ helm version 给出“无法连接到分蘖”
- visual-studio - Visual Studio 2017 未检测到 Documents 目录中的可视化工具