我正在自学 Haskell,虽然这是迄今为止最有启发性的经历,但我发现在 C 语言家族中很容易完成的一些事情仍然是个谜。所以,这是我非常基本的问题。我想要一个函数来提取具有等于给定值的某个字段的元组。到目前为止我有这个代码
withJob :: [(String, String, String)] -> String -> [String]
withJob [] _ = []
withJob ((_,_,x):xs) job
| job == x = x:(withJob xs job)
| otherwise = (withJob xs job)
users :: [(String, String, String)]
users = [("Jack", "22", "Programmer"), ("Mary", "21", "Designer"), ("John", "24", "Designer")]
当这样调用users 'withJob' "Programmer"
它时,它会输出,["Programmer"]
但我希望它输出[("Jack", "22", "Programmer")]
,但是我不知道如何访问元组而不是中的 job( x
)job == x = x:(withJob xs job)