Monty Hall problem: Difference between revisions

no edit summary
(Added Ada)
No edit summary
Line 282:
Not switching allows you to win 33337 out of 100000 times.
Switching allows you to win 66529 out of 100000 times.</pre>
 
=={{header|Scheme}}==
<pre language=scheme>
(define (random-from-list list) (list-ref list (random (length list))))
(define (random-permutation list)
(if (null? list)
'()
(let* ((car (random-from-list list))
(cdr (random-permutation (remove car list))))
(cons car cdr))))
(define (random-configuration) (random-permutation '(goat goat car)))
(define (random-door) (random-from-list '(0 1 2)))
 
(define (trial strategy)
(define (door-with-goat-other-than door strategy)
(cond ((and (not (= 0 door)) (equal? (list-ref strategy 0) 'goat)) 0)
((and (not (= 1 door)) (equal? (list-ref strategy 1) 'goat)) 1)
((and (not (= 2 door)) (equal? (list-ref strategy 2) 'goat)) 2)))
(let* ((configuration (random-configuration))
(players-first-guess (strategy `(would-you-please-pick-a-door?)))
(door-to-show-player (door-with-goat-other-than players-first-guess
configuration))
(players-final-guess (strategy `(there-is-a-goat-at/would-you-like-to-move?
,door-to-show-player))))
(if (equal? (list-ref configuration players-final-guess) 'car)
'you-win!
'you-lost)))
 
(define (stay-strategy message)
(let ((first-choice (random-door)))
(case (car message)
((would-you-please-pick-a-door?) first-choice)
((there-is-a-goat-at/would-you-like-to-move?) first-choice))))
 
(define (switch-strategy message)
(let ((first-choice (random-door)))
(case (car message)
((would-you-please-pick-a-door?) first-choice)
((there-is-a-goat-at/would-you-like-to-move?)
(car (remove first-choice (remove (cadr message) '(0 1 2))))))))
 
(define-syntax repeat
(syntax-rules ()
((repeat <n> <body> ...)
(let loop ((i <n>))
(if (zero? i)
'()
(cons ((lambda () <body> ...))
(loop (- i 1))))))))
 
(define (count element list)
(if (null? list)
0
(if (equal? element (car list))
(+ 1 (count element (cdr list)))
(count element (cdr list)))))
 
(define (prepare-result strategy results)
`(,strategy won with probability
,(exact->inexact (* 100 (/ (count 'you-win! results) (length results)))) %))
 
(define (compare-strategies times)
(append
(prepare-result 'stay-strategy (repeat times (trial stay-strategy)))
'(and)
(prepare-result 'switch-strategy (repeat times (trial switch-strategy)))))
 
;; > (compare-strategies 1000000)
;; (stay-strategy won with probability 33.3638 %
;; and switch-strategy won with probability 51.8763 %)
</pre>
Anonymous user