简体   繁体   English

Haskell-搜索元组列表,返回第二个元素

[英]Haskell - Searching list of Tuples, returning second elements

So I know many variations of this question have been asked but I've been reading posts for hours and I'm just stuck. 因此,我知道有人问过这个问题的许多变体,但是我已经读了数小时的帖子,而我只是被卡住了。 I'm getting better at haskell but not fully comprehending everything. 我在Haskell方面做得更好,但并没有完全理解所有内容。

For starters, this is an assignment so while I don't want the answer looking for some guidance. 对于初学者来说,这是一项任务,因此虽然我不希望答案寻求指导。

We have these 4 types declared for this assignment: 我们为此作业声明了以下4种类型:

type Node = Integer
type Edge = (Integer, Integer)
type Graph = [Edge]
type Path = [Node]

What I am currently working is a function which takes a Node and a Graph and returns a list of Nodes that are connected to the Node passed in. 我目前正在使用的是一个函数,该函数接受一个Node和一个Graph并返回连接到传入的Node的Node列表。

I'm really struggling with how to recursively return the second element in the tuple once I've found the source node i'm looking for. 找到我要找的源节点后,我真的在如何递归地返回元组中的第二个元素上感到很挣扎。 I know I need to be using fst to grab the first element of each tuple for comparison but then I get lost here, I'm stuck on how to correctly return the second element (add it to my list) and then proceed through the remaining list of tuples. 我知道我需要使用fst来获取每个元组的第一个元素以进行比较,但是随后我迷路了,我被困在如何正确返回第二个元素(将其添加到我的列表中)中,然后继续进行其余操作的过程元组列表。

Neighbors :: Node -> Graph -> [Node]
Neighbors nd gr = 

I've been reading about filter but I'm not sure that would work in this scenario as that returns pairs if I understand correctly. 我一直在阅读有关过滤器的信息,但不确定在这种情况下是否可以使用,因为如果我理解正确的话,它会返回对。 Looking for any guidance, thank you 寻找任何指导,谢谢

You can indeed use filter to find edges that leaves from a given node. 您确实可以使用过滤器来查找从给定节点离开的边。 Then you need to take second element of each of this edge. 然后,您需要获取每个边的第二个元素。 You need map for that. 您需要为此的地图。 So you write: 所以你写:

neighbors nd gr = map snd $ filter (\x -> fst x == nd) gr 

(Note that Haskell would not allow you to start name of the function with capital letter). (请注意,Haskell不允许您以大写字母开头的函数名称)。 If you want to be cool you can write it in almost point-free way: 如果您想变酷,可以用几乎没有意义的方式编写它:

neighbors nd  = map snd . filter ((==nd) . fst ) 

And if you want to make it easier to read you can use list comprehensions: 而且,如果您想使其更易于阅读,则可以使用列表推导:

neighbors nd gr = [ y | (x,y) <- gr, x==nd ]

filter could indeed be used to return only those edges in the graph that are of interest to you (ie, those leading out from the target node). 确实可以使用filter来返回图形中您感兴趣的那些边缘(即,从目标节点引出的那些边缘)。 Then you'd have a list of edges, when what you really want is a list of nodes. 然后,当您真正想要的是节点列表时,您将获得一条边列表。 Can you think of a function that can transform a list of edges into a list of nodes, after you've filtered down to only the edges you want? 在只过滤到所需的边缘后,您能想到一种可以将边缘列表转换为节点列表的功能吗?

edges :: [Edge]
f :: Edge -> Bool
filter f edges :: [Edge]
g :: [Edge] -> [Node]
g (filter f edges) :: [Node]

Above is a reasonable set of steps you could take, if you can think of appropriate f and g functions. 如果可以考虑适当的fg函数,则以上是可以采取的合理步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM