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 #'<) '()))

11 則留言:

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.

카지노사이트존 提到...

Thanks for sharing your info. I truly appreciate your efforts
and I will be waiting for your next write ups thanks once again.

카지노사이트 提到...

Some truly quality articles on this website , bookmarked .

카지노사이트 提到...

Just want to say your article is as surprising.

신용카드 현금화 提到...

Hi there, I enjoy reading all of your post. I wanted to write a little comment to support you.

안전놀이터 提到...

As I website possessor I believe the articles here is very excellent, thanks for your efforts.

토토사이트 提到...

Hello, it's my first go to see of this weblog; this blog includes awesome and truly excellent material for readers.