可以这样写:
head $ foldr (:) [] [1..]
-- 1
但是当我尝试处理元组时,它会进入无限循环:
head . fst $ foldr (\ x (ls, _) -> (x : ls, 0)) ([], 0) [1..]
我需要这个是因为我希望在内部函数中传递生成元素的数量。就像这样:
foldr go ([], 0) [1..]
go num (ls, cnt) = -- use cnt to get l and produce new pair (l : ls, cnt + 1)