有人可以帮我调试我的方案代码吗
can someone help me debug my scheme code
我想写一个简单的函数(最高 L k),它接受一个列表并整数 K>0,return 一个具有 k 个最高数字的新列表。
(最高'(6 7 8 5 3 2)3)给(6 7 8)
(define (highest L k)
(if (= k 0)
'()
(cons (highesthelper (car L) L)
((highest (remove (highesthelper (car L) L) L) (- k 1))))))
(define (remove E L)
(cond
((null? L)'())
((= E (car L)) (cdr L))
(else (cons (car L) (remove E (cdr L))))))
(define (highesthelper Hi L)
(cond
((null? L) Hi)
((> Hi (car L)) highesthelper Hi (cdr L))
(else (highesthelper (car L) (cdr L)))))
(highest '(1 7 4 5 3) 2)
我可以 运行 helper 和 remove 函数没有问题,但是 highest 函数给我错误,有人可以帮助我吗?
谢谢
我修正了一些简单的错误。请特别注意 highesthelper
的 (> Hi (car L))
情况。
(define (highest L k)
(if (= k 0)
'()
(cons (highesthelper (car L) L)
(highest (remove (highesthelper (car L) L) L) (- k 1)))))
(define (remove E L)
(cond
((null? L) '())
((= E (car L)) (cdr L))
(else (cons (car L) (remove E (cdr L))))))
(define (highesthelper Hi L)
(cond
((null? L) Hi)
((> Hi (car L)) (highesthelper Hi (cdr L)))
(else (highesthelper (car L) (cdr L)))))
(highest '(1 7 4 5 3) 2)
我想写一个简单的函数(最高 L k),它接受一个列表并整数 K>0,return 一个具有 k 个最高数字的新列表。 (最高'(6 7 8 5 3 2)3)给(6 7 8)
(define (highest L k)
(if (= k 0)
'()
(cons (highesthelper (car L) L)
((highest (remove (highesthelper (car L) L) L) (- k 1))))))
(define (remove E L)
(cond
((null? L)'())
((= E (car L)) (cdr L))
(else (cons (car L) (remove E (cdr L))))))
(define (highesthelper Hi L)
(cond
((null? L) Hi)
((> Hi (car L)) highesthelper Hi (cdr L))
(else (highesthelper (car L) (cdr L)))))
(highest '(1 7 4 5 3) 2)
我可以 运行 helper 和 remove 函数没有问题,但是 highest 函数给我错误,有人可以帮助我吗?
谢谢
我修正了一些简单的错误。请特别注意 highesthelper
的 (> Hi (car L))
情况。
(define (highest L k)
(if (= k 0)
'()
(cons (highesthelper (car L) L)
(highest (remove (highesthelper (car L) L) L) (- k 1)))))
(define (remove E L)
(cond
((null? L) '())
((= E (car L)) (cdr L))
(else (cons (car L) (remove E (cdr L))))))
(define (highesthelper Hi L)
(cond
((null? L) Hi)
((> Hi (car L)) (highesthelper Hi (cdr L)))
(else (highesthelper (car L) (cdr L)))))
(highest '(1 7 4 5 3) 2)