about summary refs log tree commit diff stats
path: root/dbc2.lsp
blob: 5a2699d691432894a058a6188487cd4c0111bf6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
(import "macro")                        ; For unless
(defmacro assert (test)
  `(unless ,test
     (error "assert: value is false." ',test)))
(defun my-sum (sequence)
  (let ((res 0))
    (for ((xs sequence (cdr xs)))
         ((null xs) res)
         (setq res (+ res (car xs))))))
(defun average-of-absolutes (values)
  (assure <list> values)
  ;; requires non-null list
  (assert (> (length values) 0))
  (let ((res (quotient (my-sum (mapcar #'abs values)) (length values))))
       ;; must ensure positive result
       (assert (>= res 0))
       (assure <number> res)))
;; (average-of-absolutes '(1 3))
;; (average-of-absolutes '())