Im pretty new to Haskell. I have a datatype:
data Sentence= Prop Int
| No Sentence
| And [Sentence]
| Or [Sentence]
deriving Eq
I already wrote a Show instance for it
However, whether it makes sense or not, I would like to be able to generate a random Sentence. How can i accomplish this in Haskell?
Random number generation is a typical example of an "impure" operation, since calling a random generator twice will of course yield different results - Haskell's nature disallows this.
Therefore, you need to use a so called monad Gen a
that represents a random generator, that, when run, yields a value of type a
.
Fortunately, you can combine these generators in a quite nice syntax ...
So, you just need some library that implements such a generator - and here we go.
randomNo = No <$> randomSentence
randomProp = Prop <$> choose (1, 10)
[...]
randomSentence = oneOf [randomNo, randomProp, ...]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With