我刚开始学习 Scheme(Petite Chez Scheme),作为对自己的挑战,我正在尝试实施快速排序。但是,当我运行它时出现以下异常:
Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...)
这是我在 Emacs 中的 Scheme 会话:
Petite Chez Scheme Version 8.4
Copyright (c) 1985-2011 Cadence Research Systems
> (define (set a i k)
(define (reduce-list a i n)
(if(= i n)
a
(reduce-list (cdr a) (+ i 1) n)))
(if(= i 0)
(cons k (cdr a))
(let ((b (cons k (reduce-list a 0 (+ i 1)))))
(let push-front ((lst b) (original-list a) (n (- i 1)))
(if(<= n 0)
(cons (list-ref original-list 0) lst)
(push-front (cons (list-ref original-list n) lst) original-list (- n 1)))))))
(define (swap lst i j)
(let ((a (list-ref lst i))
(b (list-ref lst j)))
(set (set lst i b) j a)))
(define (partition a i j r)
(cond [(= j r) (cons (+ i 1) (swap a (+ i 1) j))]
[(<= (list-ref a j) (list-ref a r)) (partition (swap a j (+ i 1)) (+ i 1) (+ j 1) r)]
[else (partition a i (+ j 1) r)]))
(define (quicksort a p r)
(if(< p r)
(begin(
(let* ((c (partition a (- p 1) p r))
(q (car c))
(b (quicksort (cdr c) p (- q 1))))
(quicksort b (+ q 1) r))))
a))
> (define lst (list 1 9 2 8 3 7 4 6 5))
> (define n (length lst))
> (trace quicksort)
(quicksort)
> (quicksort lst 0 (- n 1))
|(quicksort (1 9 2 8 3 7 4 6 5) 0 8)
| (quicksort (1 2 3 4 5 7 8 6 9) 0 3)
| |(quicksort (1 2 3 4 5 7 8 6 9) 0 2)
| | (quicksort (1 2 3 4 5 7 8 6 9) 0 1)
| | |(quicksort (1 2 3 4 5 7 8 6 9) 0 0)
| | |(1 2 3 4 5 7 8 6 9)
| | |(quicksort (1 2 3 4 5 7 8 6 9) 2 1)
| | |(1 2 3 4 5 7 8 6 9)
Exception: attempt to apply non-procedure (1 2 3 4 5 7 ...)
>
谁能告诉我出了什么问题?先感谢您