Common Lisp/Дерева/Відповіді

Матеріал з Вікіпідручника

Common Lisp/Дерева:

1. $ (DEFUN l (tree)$ (DEFUN leaves (tree)

(SETQ Counter1 0)((AND (NULL (CADR tree)) (NULL (CDDR tree)))

(leaves tree)(INCQ Counter1))

Counter1 )(IF (NOT (NULL (CADR tree))) (leaves (CADR tree)))

(IF (NOT (NULL (CDDR tree))) (leaves (CDDR tree))) )


2. $ (DEFUN count (tree)

((NULL tree) 0)

(+ 1 (count (CADR tree)) (count (CDDR tree))) )

$ (DEFUN sm (tree)

((NULL tree) 0)

(+ (CAR tree) (sm (CADR tree)) (sm (CDDR tree))) )

$ (DEFUN average (tree)

(/ (sm tree) (count tree)) )


3. $ (DEFUN QSORT (lst)

((NULL (CDR lst)) lst)

((NULL (CDDR lst))

((< (CAR lst) (CADR lst)) lst)

(CONS (CADR lst) (CONS (CAR lst) NIL)))

(SETQ tmp1 (SPLIT lst))

(APPL (QSORT tmp1) (QSORT lst)) )