Haskell如何评估这个撤消列表插入的函数?

时间:2014-04-22 23:38:02

标签: haskell equational-reasoning

我正在尝试了解Haskell如何评估sep [1, 2, 3, 4, 5]以获取([1, 3], [2, 4, 5])

sep [ ] = ([ ], [ ])
sep [x] = ([ ], [x])
sep (x1:x2:xs) = let (is, ps) = sep xs in (x1:is, x2:ps)

我是这样开始的:

sep [1, 2, 3, 4, 5] = let (is, ps) = sep [3, 4, 5] in (1:is, 2:ps)

但是呢?

1 个答案:

答案 0 :(得分:4)

最后我明白了。

1) sep [1, 2, 3, 4, 5] = let (is, ps) = sep [3, 4, 5] in (1:is, 2:ps)

2) sep [3, 4, 5] = let (is, ps) = sep [5] in (3:is, 4:ps)

3) sep [5] = ([], [5])

2)sep [3, 4, 5] = let (is, ps) = ([], [5]) in (3:is, 4:ps) = ([3], [4, 5])

1)sep [1, 2, 3, 4, 5] = let (is, ps) = ([3], [4, 5]) in (1:is, 2:ps) = ([1, 3], [2, 4, 5])