我定义了以下数据类型:
data NewBool = Truth | Lie deriving (Show)
我创建了一个函数,它应该返回一个随机的 NewBool 值
giveMeBool :: IO()
giveMeBool = do
bool <- randomIO :: IO NewBool
putStrLn("Your random boolean is"++ show bool)
我在这里读到,我必须使 NewBool 成为 Random 的实例才能使用 randomIO。所以我这样做了:
instance Random NewBool where
random g = case random g of
(r,g') | r < (1/2)= (Truth, g')
| otherwise= (Lie, g')
真诚地,这只是我在另一篇文章中找到的内容的复制和粘贴,但我不明白它是如何工作的?r 的值和类型是如何定义的?谁能帮我?谢谢 ;)