0 Members and 1 Guest are viewing this topic.
ElpanovEvgeniyI think you got it. You started with a hard one. I tried for about 15 minutes but no easy solution.Perhaps you can post your solution.
(defun rec-min (lst mi f) ;(rec-min (cdr lst) (car lst) f) (cond ((not lst) mi) (((eval f) (car lst) mi) (rec-min (cdr lst) (car lst) f) ) (t (rec-min (cdr lst) mi f)) ) ;_ cond) ;_ defun(defun rec-remove-singl (i lst) ;(rec-remove-singl (cadr lst) lst) (if lst (if (equal i (car lst)) (cdr lst) (cons (car lst) (rec-remove-singl i (cdr lst))) ) ;_ if ) ;_ if) ;_ defun(defun rec-sort-min (lst f) ;(rec-sort-min lst) (if lst ((lambda (x) (cons x (rec-sort-min (rec-remove-singl x lst ) f ) ) ) ;_ lambda (rec-min (cdr lst) (car lst) f) ) ) ;_ if) ;_ defun ; Check:(setq lst '(7 3 4 6 9 6 7 2 5 3 2 3 6 4 6 3 1) f (function <)) ;_ setq(rec-sort-min lst f)
(defun rec-min-max (lst mi ma f) (cond ((not lst) (list mi ma)) (((eval f) (car lst) mi) (rec-min-max (cdr lst) (car lst) ma f) ) (((eval f) ma (car lst)) (rec-min-max (cdr lst) mi (car lst) f) ) (t (rec-min-max (cdr lst) mi ma f)) ) ;_ cond) ;_ defun(defun rec-remove-singl (i lst) (if lst (if (equal i (car lst)) (cdr lst) (cons (car lst) (rec-remove-singl i (cdr lst))) ) ;_ if ) ;_ if) ;_ defun(defun rec-sort-min-max (lst f) (cond ((not lst) nil) ((not(cdr lst)) lst) (t ((lambda (x) (cons (car x) (append (rec-sort-min-max (rec-remove-singl (car x) (rec-remove-singl (cadr x) lst ) ;_ rec-remove-singl ) ;_ rec-remove-singl f ) ;_ rec-sort-lists (cdr x) ) ;_ append ) ;_ cons ) ;_ lambda (rec-min-max (cdr lst) (car lst) (car lst) f) ) ) ) ;_ cond) ;_ defun ; Check:(setq lst '(7 3 4 6 9 6 7 2 5 3 2 3 6 4 6 3 1) f (function <)) ;_ setq(rec-sort-min-max lst f)
(defun rec-quicksort-2 (lst lst1 lst2 test f) (cond ((not lst) (list lst1 (list test) lst2) ) (((eval f) (car lst) test) (rec-quicksort-2 (cdr lst) (cons (car lst) lst1) lst2 test f) ) (t (rec-quicksort-2 (cdr lst) lst1 (cons (car lst) lst2) test f)) ) ;_ cond) ;_ defun(defun rec-quicksort-1 (lst f) (cond ((not lst) nil) ((not (car lst)) (rec-quicksort-1 (cdr lst) f)) ((not (cdar lst)) (cons (caar lst) (rec-quicksort-1 (cdr lst) f)) ) ((not (cddar lst)) (if (apply f (car lst)) (cons (caar lst) (cons (cadar lst) (rec-quicksort-1 (cdr lst) f))) (cons (cadar lst) (cons (caar lst) (rec-quicksort-1 (cdr lst) f))) ) ;_ if ) (t ((lambda (x) (rec-quicksort-1 (cons (car x) (cons (cadr x) (cons (caddr x) (cdr lst)))) f) ) ;_ lambda (rec-quicksort-2 (cdar lst) nil nil (caar lst) f) ) ) ) ;_ cond) ;_ defun(defun rec-quicksort (lst f) (rec-quicksort-1 (list lst) f)) ;_ defun ; Check:(setq lst '(7 3 4 6 9 6 7 2 5 3 2 3 6 4 6 3 1) f (function <)) ;_ setq(rec-quicksort lst f)