Monty Hall problem: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: added syntax colouring, marked p2js compatible)
(Emacs Lisp: Improve formatting)
Line 1,669: Line 1,669:
=={{header|Emacs Lisp}}==
=={{header|Emacs Lisp}}==
{{trans|Picolisp}}
{{trans|Picolisp}}
<lang lisp>
<lang lisp>(defun montyhall (keep)
(let ((prize (random 3))
(defun montyhall (keep)
(choice (random 3)))
(let
((prize (random 3))
(choice (random 3)))
(if keep (= prize choice)
(if keep (= prize choice)
(/= prize choice))))
(/= prize choice))))



(let ((cnt 0))
(let ((cnt 0))
(dotimes (i 10000)
(dotimes (i 10000)
(and (montyhall t) (setq cnt (1+ cnt))))
(and (montyhall t) (setq cnt (1+ cnt))))
(princ (format "Strategy keep: %.3f %%" (/ cnt 100.0))))
(message "Strategy keep: %.3f%%" (/ cnt 100.0)))


(let ((cnt 0))
(let ((cnt 0))
(dotimes (i 10000)
(dotimes (i 10000)
(and (montyhall nil) (setq cnt (1+ cnt))))
(and (montyhall nil) (setq cnt (1+ cnt))))
(princ (format "Strategy switch: %.3f %%" (/ cnt 100.0))))
(message "Strategy switch: %.3f%%" (/ cnt 100.0)))</lang>
</lang>


{{out}}
{{out}}

<pre>
Strategy keep: 34.410 %
Strategy keep: 34.410%
Strategy switch: 66.430 %
Strategy switch: 66.430%
</pre>


=={{header|Erlang}}==
=={{header|Erlang}}==