首页

2009年5月14日星期四

The use recursion sorting

The use recursion sorting

Generally recursion:
------------------------------------------------------------------------------------
(define (f x)
(if (null? (cdr x))
(list (car x))
(append (f (cdr x)) (list (car x)))))
------------------------------------------------------------------------------------
Tail recursion:
------------------------------------------------------------------------------------
(define (f x)
(let loop ((n x) (l '()))
(if (null? (cdr n)) (cons (car n) l)
(loop (cdr n) (cons (car n) l)))))
------------------------------------------------------------------------------------
Equal tail recursion :
------------------------------------------------------------------------------------
(define (foo x)
(let loop((n x) (l '()))
(if (null? (cdr n)) (append (list (car n)) l)
(loop (cdr n) (append (list (car n))l)))))
------------------------------------------------------------------------------------
This is the Scheme/Lisp procedure !!

没有评论:

发表评论