I am new to Haskell and want to generate an Arbitrary tree.
So my first idea is to create an arbitary bool, if it is true then return an empty tree and else create a non-empty one:
instance (Arbitrary a) => Arbitrary (BinaryTree a) arbitrary = do createNonEmpty <- arbitrary if createNonEmpty then return Nil else generateNonEmptyTree But this pattern of creating the bool and use it just for that if seems a bit odd and it feels like there should be a more idiomatic way.
Is there already some kind of "monadic if" in the standard library that I could use like
arbitrary = ifM arbitrary (return Nil) (generateNonEmptyTree) Or what else is the most idiomatic way to solve this?
ifMis indeed a very popular idea that exists in a number of "general-purpose" libraries, check out Stackage.Arbitraryfrom the non-standard QuickCheck I believe.