[plt-scheme] Combining two definition into one only

From: Jon Rafkind (workmin at ccs.neu.edu)
Date: Sun Oct 5 16:50:32 EDT 2008

SamuelXiao wrote:
> I am a scheme newbie, could anyone tell me how can i combine this
> following 2 definition into one?
> -----------------------------------------------------------------------------------------------------------------------------
(define insertion-sort-numbers
(lambda (numbers)
(define insert-number
(lambda (new-element sorted)
(cond ((null? sorted) (list new-element))
((<= new-element (car sorted)) (cons new-element sorted))
(else (cons (car sorted) (insert-number new-element (cdr sorted)))))))
(let helper ((unsorted numbers) ; The remaining unsorted values
(sorted '())) ; The sorted values
(if (null? unsorted)
(helper (cdr unsorted) (insert-number (car unsorted) sorted))))))

Posted on the users mailing list.