Question about haskell ( The Monty Hall problem)

  • MHB
  • Thread starter kyle2015
  • Start date
  • Tags
    Monty hall
In summary, Haskell is a programming language that is very interesting. This program is very useful for simulating the Monty Hall problem. I found this question from a book. Can you give me some tips and answer?
  • #1
kyle2015
1
0
I just study Haskell. This program is very interesting. I find this question from book. Can you give me some tips and answer? -- We're going to build a simulation of the Monty Hall problem. We'll use
-- lists to represent the state of the doors (0 = empty, 1 = prize), so here
-- are all the possible configurations of doors:
doors :: [[Int]]
doors = [[1,0,0], [0,1,0], [0,0,1]]

-- For each door-configuration, there are three possible player-choices. Our
-- goal here is to pair each door with all three possible choices. We will
-- represent the combination of a door-config and a choice as a pair:
-- (choice, [doors...]). Complete the definition of the possible choices.
-- (Note that the resulting list should have 9 entries.)
choices :: [(Int,[Int])]
choices = undefined

-- other x y
-- `other` is a helper function for figuring out which door is the "other" door.
-- Given x and y, both 0 <= x,y < 3, with x /= y, `other x y` will return the
-- other value in that range that is not equal to either x or y.
other :: Int -> Int -> Int
other x y | x < 0 || y < 0 || x >= 3 || y >= 3 || x == y =
error "Invalid arguments to other."
other 0 y = (1 - y) + 2 -- y is either 1 or 2
other 1 y = (1 - (y `div` 2)) * 2 -- y is either 0 or 2
other 2 y = 1 - y -- y is either 0 or 1

-- indexOf a l
-- If you need to find which door is the winning door, you can use the built-in
-- function indexOf, which returns the index of a in the list l.
indexOf :: Eq a => a -> [a] -> Int
indexOf a (a':as) | a == a' = 0
| otherwise = 1 + indexOf a as

-- Now comes the tricky part: given an arrangement of doors and the player's
-- choice, the host's choice is almost determined, *except* in the case where
-- the player chose correctly. We'll write a function that will give us the
-- possible host choice(s), for each player choice+door. The idea is that
-- given
-- (player choice, [doors])
-- we will return
-- [(player choice, host choice, [doors]),
-- (player choice, host choice, [doors])]
-- where the player choice and the doors are the same, but the host's choice
-- is whatever it was forced to be. Note that the list has two elements, both
-- identical; this is so that the probabilities will work out correctly later.
-- In the case where the host has two choices, we'll return a list with *two*
-- *different* elements:
-- [(player choice, host choice 1, [doors]),
-- (player choice, host choice 2, [doors])]
host_chooses :: (Int,[Int]) -> [(Int,Int,[Int])]
host_chooses (pc, doors) = undefined

-- Now, for every possible choice in `choices`, we apply host_chooses to it
-- and stick all the results together to get a big list of all the possible ways
-- things could work out, for each doors arrangement, player choice (pc), and
-- host choice (hc).
-- (pc, hc, doors)
all_possibilities :: [(Int,Int,[Int])]
all_possibilities = undefined

-- Given all the possibilities, what is the probability that the player's
-- first choice is correct (will result in a win)?
player_stay_prob = undefined

-- What is the probability that the "switch" choice would be correct?
player_switch_prob = undefined
 
Technology news on Phys.org
  • #2
-- Answer:-- choices :: [(Int,[Int])]choices = [(0, [1, 0, 0]), (1, [1, 0, 0]), (2, [1, 0, 0]), (0, [0, 1, 0]), (1, [0, 1, 0]), (2, [0, 1, 0]), (0, [0, 0, 1]), (1, [0, 0, 1]), (2, [0, 0, 1])]host_chooses :: (Int, [Int]) -> [(Int, Int, [Int])]host_chooses (pc, doors) | doors !! pc == 1 = [(pc, other pc pc, doors)] | otherwise = [(pc, other pc 0, doors), (pc, other pc 1, doors)]all_possibilities :: [(Int, Int, [Int])]all_possibilities = concatMap host_chooses choicesplayer_stay_prob = length (filter (\(_, _, doors) -> doors !! 0 == 1) all_possibilities) / length all_possibilitiesplayer_switch_prob = length (filter (\(_, _, doors) -> doors !! 1 == 1) all_possibilities) / length all_possibilities-- Tips: -- 1. Break the problem down into smaller pieces and try to solve them one by one.-- 2. Think about how you can use lists and list functions to solve the problem.-- 3. Test your code as you go along to make sure it is working as expected.
 

FAQ: Question about haskell ( The Monty Hall problem)

What is the Monty Hall problem?

The Monty Hall problem is a famous probability puzzle that is based on a game show scenario. It is named after the host of the game show "Let's Make a Deal", Monty Hall. In the problem, there are three doors, behind one of which is a valuable prize. The contestant is asked to choose one door, and then the host, who knows where the prize is, opens one of the remaining doors to reveal a non-prize. The contestant is then given the option to switch their choice to the other unopened door. The question is, is it better to switch or stick with your original choice?

What is the solution to the Monty Hall problem?

The solution to the Monty Hall problem is that it is better to switch your choice. This may seem counterintuitive, as it may seem that there are only two doors left and the chances are 50/50. However, by switching, the contestant actually increases their chances of winning to 2/3.

Why is it better to switch in the Monty Hall problem?

The reason it is better to switch in the Monty Hall problem is because the host's actions provide new information. By opening one of the doors, the host is essentially giving the contestant a free hint. If the contestant's initial choice was wrong, then the other unopened door must hold the prize. Therefore, by switching, the contestant has a higher chance of choosing the correct door.

Are there any real-life applications of the Monty Hall problem?

While the Monty Hall problem may seem like a purely mathematical puzzle, it has real-life applications in fields such as decision-making, game theory, and even cryptography. It highlights the importance of considering all available information before making a decision, and how seemingly simple choices can have unexpected consequences.

Is there a similar problem to the Monty Hall problem?

Yes, there are several variations of the Monty Hall problem, such as the "Monty Fall" problem where the host purposefully chooses the door with the prize, or the "Monty Stall" problem where the host has the option to open the door with the prize but chooses not to. These variations can have different solutions and can further illustrate the importance of considering all available information in decision-making.

Similar threads

Replies
15
Views
2K
Replies
12
Views
2K
Replies
1
Views
2K
Replies
212
Views
13K
Replies
6
Views
2K
Replies
9
Views
2K
Replies
19
Views
3K
Back
Top