我试图使用 Haskell 使用通配符查找元组列表中元素的索引。
我调整了我的类型以使其更容易解释。目前我的思路是使用elemIndex。
例如,
> elemIndex 2 [1,2,3,4,5]
Just 1
Run Code Online (Sandbox Code Playgroud)
但就我而言,我有一个元组列表,如下所示:
> elemIndex (2, 20) [(1, 10),(2, 20),(3, 30),(4, 40),(5,50)]
Just 1
Run Code Online (Sandbox Code Playgroud)
我希望能够只提供元组的第一部分并获取索引,如下所示:
> elemIndex (2, _) [(1, 10),(2, 20),(3, 30),(4, 40),(5,50)]
Just 1
Run Code Online (Sandbox Code Playgroud)
但我得到的结果是这样的:
<interactive>:58:15: error:
• Found hole: _ :: Integer
• In the expression: _
In the first argument of ‘elemIndex’, namely ‘(2, _)’
In the expression:
elemIndex (2, _) [(1, 10), (2, 20), (3, 30), (4, 40), ....]
• Relevant bindings include
it :: Maybe …Run Code Online (Sandbox Code Playgroud)