嗨,任何人都可以向我解释这些代码是如何得出他们的答案的?
pip = phi 4 where phi x = if x == 1 then 1 else 1+ phi (x - 1) * phi (x - 1)
这将返回26的结果.
dpip = phi 5 where phi x = if x == 1 then 1 else 1+ phi (x - 1) * phi (x - 1)
这将返回677的结果.
也是一段类似的代码
plip = phi 4 where phi x = 1 + sum [ phi y | y <- [1.. (x-1)]]
返回8的结果
plips = phi 5 where phi x = 1 + sum [ phi y | y <- [1.. (x-1)]]
返回16的结果.
我真的不知道这些结果是如何实现的.
让我们分解他们
pip = phi 4 where phi x = if x == 1 then 1 else 1+ phi (x - 1) * phi (x - 1)
可分为两行
pip = phi 4 phi x = if x == 1 then 1 else 1+ phi (x - 1) * phi (x - 1)
第一行很简单,第二行是递归定义
phi 4 = 1 + phi 3 * phi 3 phi 3 = 1 + phi 2 * phi 2 phi 2 = 1 + phi 1 * phi 1 phi 1 = 1
并代替链条
phi 1 = 1 phi 2 = 1 + 1 * 1 = 2 phi 3 = 1 + 2 * 2 = 5 phi 4 = 1 + 5 * 5 = 26
你的下一个问题只是向上走了一步
phi 5 = 1 + phi 4 * phi 4 = 1 + 26 * 26 = 677
类似的分析适用于第二段代码.