[Haskell-beginners] Need better explanation of the 'flipThree' example in LYAH

Olumide 50295 at web.de
Tue Aug 21 00:04:01 UTC 2018


Dear List,

I'm trying to understand the following example from LYAH

     import Data.List (all)

     flipThree :: Prob Bool
     flipThree = do
         a <- coin
         b <- coin
         c <- loadedCoin
         return (all (==Tails) [a,b,c])

Where
     import Data.Ratio
     newtype Prob a = Prob { getProb :: [(a,Rational)] } deriving Show

and

     data Coin = Heads | Tails deriving (Show, Eq)

     coin :: Prob Coin
     coin = Prob [(Heads,1%2),(Tails,1%2)]

     loadedCoin :: Prob Coin
     loadedCoin = Prob [(Heads,1%10),(Tails,9%10)]

The result:

     ghci> getProb flipThree
     [(False,1 % 40),(False,9 % 40),(False,1 % 40),(False,9 % 40),
      (False,1 % 40),(False,9 % 40),(False,1 % 40),(True,9 % 40)]

See http://learnyouahaskell.com/for-a-few-monads-more#making-monads.

My understanding of what's going on here is sketchy at best. One of 
several explanations that I am considering is that all combination of a, 
b and c are evaluated in (==Tails) [a,b,c] but I cannot explain how the 
all function creates 'fuses' the list [f a, f b, f c]. I know that all f 
xs = and . map f xs (the definition on hackage is a lot more 
complicated) but, again, I cannot explain how the and function 'fuses' 
the list [f a, f b, f c].

If I'm on the right track I realize that I'm going to have to study the 
list the between list comprehensions and the do-notation in order how 
all the return function create one Prob.

Regards,

- Olumide



More information about the Beginners mailing list