- #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
-- 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