2008年11月16日 星期日

LISP Practice

Quick-sort


(defmacro for-all (x op xs)
`(remove-if-not #'(lambda (y) (,op y x)) xs))

(defun qsort (nums)
(if (not nums)
()

(let ((x (first nums))
(xs (rest nums)))
(if (eq x nil)
()

(append
(qsort (for-all x < xs))
(list x)
(qsort (for-all x >= xs)))))))


Permutation

(defun remove-first (n nums)
(remove n nums :count 1))

(defun join-sublists (superlist)
(reduce #'(lambda (l1 l2) (append l1 l2)) superlist))

(defun permute-sorted-list (nums perm)
(if (eq nums nil)
(list perm)

(let ((unique-nums (remove-duplicates nums))) (join-sublists
(mapcar (lambda (n)
(permute-sorted-list (remove-first n nums) (append perm (list n))))
unique-nums)))))

(defun permutation (nums)
(permute-sorted-list (sort nums #'<) '()))

5 則留言:

Pennington 提到...

You are terrific. I like this remarkable things, Keep it up and keep on writing

Fernando 提到...

I like to see some other posts on the same subject! Very useful informative info!!

Velazquez 提到...

I must thank you for the efforts you’ve put in writing this blog. Awesome!

Macdonald 提到...

You’ve written nice post, I am gonna bookmark this page, thanks for info.

Schroeder 提到...

Hi very nice blog! I'll bookmark this website. Thanks for blogging.