about summary refs log blame commit diff stats
path: root/dbc2.lsp
blob: 5a2699d691432894a058a6188487cd4c0111bf6a (plain) (tree)
1
2
3
4
5
6
7
8
9
                                                    
                       


                                               

                                 

                                       
                                    
                        
                           
                                
                                                                        
                                     
                          
                              

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