100 doors: Difference between revisions
Content added Content deleted
(link no longer goes to the right place) |
(Just the straightforward way please) |
||
Line 1,746: | Line 1,746: | ||
(print-doors (do-all-visits doors size open shut))))</lang> |
(print-doors (do-all-visits doors size open shut))))</lang> |
||
'''Unoptimized |
'''Unoptimized, imperative ''' |
||
This is a version that closely follows the problem description and is still quite short. |
This is a version that closely follows the problem description and is still quite short. Of all the presented solutions it might be closest to "idiomatic Common Lisp". |
||
<lang lisp>(define-modify-macro toggle () not) |
<lang lisp>(define-modify-macro toggle () not) |
||
Line 1,759: | Line 1,759: | ||
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))</lang> |
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))</lang> |
||
'''Optimized |
'''Optimized''' |
||
This is an optimized version of the above, using the perfect square algorithm |
This is an optimized version of the above, using the perfect square algorithm. |
||
<lang-lisp>(defun 100-doors () |
|||
(let ((doors (make-array 100 :initial-element nil))) |
|||
(dotimes (i 10) |
|||
(setf (svref doors (* i i)) t)) |
|||
(dotimes (i 100) |
|||
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))</lang> |
|||
'''Optimized 2''' |
|||
Another optimized version, with finer granular separation of functionality (might be a bit excessive). |
|||
<lang lisp>(defun perfect-square-list (n) |
<lang lisp>(defun perfect-square-list (n) |
||
Line 1,787: | Line 1,797: | ||
'''Optimized (2) ''' |
'''Optimized (2) ''' |
||
This version displays a much more functional solution through the use of MAPCAR |
This version displays a much more functional solution through the use of MAPCAR. |
||
<lang lisp>(let ((i 0)) |
<lang lisp>(let ((i 0)) |