Count the coins: Difference between revisions

→‎{{header|Common Lisp}}: simpler and more idiomatic code
(Updated D entry n.4)
(→‎{{header|Common Lisp}}: simpler and more idiomatic code)
Line 387:
 
=={{header|Common Lisp}}==
<lang lisp>(defun count-change (amount coins)
&optional
(let ((cache (make-array (list (1+ amount) (length coins))
((recur (n coins &optional (l (1- (length coins))))
:initial-element nil)))
(let ( (cache (make-array (list (1+ amount) (length coins))
(macrolet ((h () `(aref cache n l)))
:initial-element nil)))
(labels
(cond ((< l 0) 0)
((recur (n coins &optional (l (1- (length coins))))
(cond ((< lamount 0) 0)
((<= namount 0) 01)
(t (or (=aref ncache 0)amount 1l)
(t (if (h)setf (h)aref ;cache cachedamount l)
(+ (count-change (- amount (first coins)) coins l cache)
(setf (h) ; or not
(+ (recur (count-change namount (carrest coins)) coins(1- l) cache)))))))
(recur n (cdr coins) (1- l)))))))))
 
;; enable next line if recursions too deep
;(loop for i from 0 below amount do (recur i coins))
(recur amount coins)))))
 
; (compile 'count-change) ; for CLISP
Anonymous user