在Haskell中,我可能会if
这样实现:
if' True x y = x
if' False x y = y
spin 0 = ()
spin n = spin (n - 1)
这符合我的预期:
haskell> if' True (spin 1000000) () -- takes a moment
haskell> if' False (spin 1000000) () -- immediate
在Racket中,我可以实现这样的缺陷if
:
(define (if2 cond x y) (if cond x y))
(define (spin n) (if (= n 0) (void) (spin (- n 1))))
这符合我的预期:
racket> (if2 #t (spin 100000000) (void)) -- takes a moment
racket> (if2 #f (spin 100000000) (void)) -- takes a moment
在Idris中,我可能会if
这样实现:
if' : Bool -> a -> a -> a
if' True x y = x
if' False x y = y
spin : Nat -> ()
spin Z = ()
spin (S n) = spin n
这种行为让我感到惊讶:
idris> if' True (spin 1000) () -- takes a moment
idris> if' False (spin 1000) () -- immediate
我希望 Irdis 表现得像 Racket,两个参数都被评估。但事实并非如此!
伊德里斯如何决定何时评估事物?