Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 有没有办法计算元组列表的第一个索引出现的次数_List_Haskell_Tuples - Fatal编程技术网

List 有没有办法计算元组列表的第一个索引出现的次数

List 有没有办法计算元组列表的第一个索引出现的次数,list,haskell,tuples,List,Haskell,Tuples,我试图在Haskell中编写一个函数,它接受一个元组列表(每个元组的第一个索引是int,第二个索引是char)和一个整数,并返回每个元组的第一个索引中出现的次数。到目前为止,我已经: counter :: Eq a => [a] -> a -> Int counter [] find = 0 counter ys find = length xs where xs = [xs | xs <- ys, xs == find] 计数器::Eq a=>[a]->a-

我试图在Haskell中编写一个函数,它接受一个元组列表(每个元组的第一个索引是int,第二个索引是char)和一个整数,并返回每个元组的第一个索引中出现的次数。到目前为止,我已经:

counter :: Eq a => [a] -> a -> Int 
counter [] find = 0
counter ys find = length xs
    where xs = [xs | xs <- ys, xs == find]
计数器::Eq a=>[a]->a->Int
计数器[]查找=0
计数器ys find=长度xs
其中xs=[xs | xs需要解包2元组并检查第一项,因此:

counter :: Eq a => [(a, b)] -> a -> Int 
counter ys find = length [ x | (x,_) <- ys, x == find]

非常感谢,这是非常有用的
counter :: Eq a => [(a, b)] -> a -> Int 
counter ys find = length (filter ((find ==) . fst) ys)