100 doors: Difference between revisions

Just the straightforward way please
(link no longer goes to the right place)
(Just the straightforward way please)
Line 1,746:
(print-doors (do-all-visits doors size open shut))))</lang>
 
'''Unoptimized /, imperative '''
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)
Line 1,759:
(format t "door ~a: ~:[closed~;open~]~%" (1+ i) (svref doors i)))))</lang>
 
'''Optimized '''
This is an optimized version of the above, using the perfect square algorithm (Note: This is non-functional as the state of the doors variable gets modified by a function call).
 
<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)
Line 1,787 ⟶ 1,797:
 
'''Optimized (2) '''
This version displays a much more functional solution through the use of MAPCAR (note however that this is imperative as it does variable mutation).
 
<lang lisp>(let ((i 0))
Anonymous user