【发布时间】:2016-11-16 00:33:47
【问题描述】:
我是 Racket 的新手,我正在尝试找出插入排序。这就是我所拥有的,但我遇到了一个错误,我无法从调试中弄清楚。
;Definition of insert: inserts a number into an already sorted list based on
;the cmp parameter
;cmp: < or >, L1: a list, n: the number to be inserted
(define (insert cmp L1 n)
(cond
((null? n) (list L1))
((null? L1) (cons n L1))
((cmp n (car L1)) (cons n L1))
(else (cons (car L1) (insert cmp (cdr L1) n)))
)
)
;Definition of insertionSort: sorts a list based on a recursive insertion sort
;L1: a list, cmp: < or >
(define (insertionSort L1 cmp)
(cond
((null? L1) L1)
(else (insert cmp (car L1) (insertionSort(cdr L1) cmp)))
)
)
【问题讨论】:
-
(null? n)中的insert不是很有用,因为n是一个数字。
标签: recursion scheme racket insertion-sort