使用`map`遍历递归数据结构时如何通过总计检查

时间:2019-04-29 08:22:43

标签: idris

例如,我有以下代码:

data Tree = Branches (List Tree) | Node Int

sumTree : Tree -> Int
sumTree (Branches xs) = sum (map sumTree xs)
sumTree (Node x) = x

如果我希望它通过总体检查,则必须手动展开地图:

data Tree = Branches (List Tree) | Node Int

sumTree : Tree -> Int
sumTree (Branches xs) = sum' xs where
  sum' [] = 0
  sum' (x::xs) = sumTree x + sum' xs
sumTree (Node x) = x

如果我想避免在所有地方都使用assert_total,那么函数式编程就可以发挥很多功能。有什么方法可以解决此问题?

0 个答案:

没有答案