开发者

Scheme: redefining built-ins

开发者 https://www.devze.com 2023-01-28 17:47 出处:网络
I\'m having trouble with functions that are n-ary, for example, =, <, etc. I\'m trying to implement = first of all. All I have is the following (it\'s n开发者_如何学运维ot very much).

I'm having trouble with functions that are n-ary, for example, =, <, etc. I'm trying to implement = first of all. All I have is the following (it's n开发者_如何学运维ot very much).

(define builtin-= =)
(define (b= x y) (builtin-= x y))

(define (= . z)
 (if (null? z) #f
  (b= (car z) (apply = (cdr z)))))

However, this results in either infinite recursion or heap overflow when I run (= 2 3) or any other similar call.


Thanks.

I figured it out.

(define builtin-= =)
(define (b= x y) (builtin-= x y))

(define (= . z)
  (if (or (null? z) (null? (cdr z)))
    #t
    (and (equal? (car z) (cadr z))
         (= (cdr z)))))

I have another problem though. I'm trying to do this for < also. So, equal? won't work.

0

精彩评论

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