开发者

Scheme - printing to screen - Normal eval

开发者 https://www.devze.com 2023-02-28 18:20 出处:网络
Why in the next code, nothin开发者_JAVA技巧g is displayed when we work on Normal eval (lazy one).

Why in the next code, nothin开发者_JAVA技巧g is displayed when we work on Normal eval (lazy one).

;;; [Number, Number -> Number]
(define (printing-sub x y)
   (display x)
   (- x y))

;;; [Number, Number -> Number] 
(define (f a b)
   (if (< a 0)
 a
       (f b (printing-sub b a))))
(f 0 0)

I'll exmplain: In the first iteration, we will got (f 0 (printing-sub 0 0)), in the 2nd: (f (prining-sub 0 0) (prining sub (printing sub 0 0) 0). Now, in the 3rd, we have to calculate (printing-sub 0 0) because we want to know (if (<a 0). In this iteration 0 will print out.

What am I missing?

Thank you.


I too don't know what you mean by "normal eval", but I don't understand why you would expect anything but 0 to be printed out with that code. In fact it'll cause an infinite loop, printing out endless zeroes.

Note that (printing-sub 0 0) will always just display 0 and return 0, because (- 0 0) is 0. So in the first iteration you get (f 0 (printing-sub 0 0)) which reduces back to (f 0 0) which causes the infinite loop.

In other words, the if always evaluates to #f because a will never become anything other than 0.

0

精彩评论

暂无评论...
验证码 换一张
取 消