我对 If Then Else 定义中的预测有疑问。它实际上是作为 If-Else-Then 执行的。
import Prelude hiding (pred,and,or,not)
data PR = Z
| S
| P Int
| C PR [PR]
| PR PR PR
deriving Show
eval :: PR -> [Integer] - Integer
eval Z _ = 0
eval S [x] = x+1
eval (P n) xs = nth n xs
eval (C f gs) xs = eval f (map (\g -> eval g xs) gs)
eval (PR g h) (0:xs) = eval g xs
eval (PR g h) (x:xs) = eval h ((x-1) : eval (PR g h) ((x-1):xs) : xs)
nth _ [] = error "nth nil"
nth 0 _ = error "nth index"
nth 1 (x:_) = x
nth (n) (_:xs) = nth (n-1) xs
one = C S [Z]
plus = PR (P 1) (C S [P 2])
ife = PR (P 1) (C (P 2) [P 3, P 4])
如果我尝试交换P 3
并且P 4
它完全中断(每次返回'then'值)。ite[0,2,3]
应该返回3
并且ite[1,2,3]
应该返回2
。相反,正在发生相反的情况。我该如何纠正?