Grasping `all-the-apples-at-once' › 解法样本
Sample solutions
› Haskell (Group II) [#287]
Grasping `all-the-apples-at-once' › 解法样本 Sample solutions › Haskell (Group II) [#287]
main = interact soln
soln = show . reverse . go []
go counts [] = counts
go counts (x:xs) = go (inc x counts) xs
inc c ((x,ct):xs) | c == x = (x,ct+1):xs
inc c xs = (c, 1):xs
尽管该解法依赖于显式递归而不是
fold
,但它与 Erlang 解法类似。(后来有另一个人提到了另一种使用 group
的解法)。
Although this solution relies on the explicit recursion rather than fold, it is rather similar to the Erlang solution. (A different person later mentioned another solution, using group).