haskell - How do I create an arbitrary instance for a recursive datatype? -


i think creates arbitrary lists of length three, how create lists of arbitrary length?

import test.quickcheck  data list =   nil   | cons (list a)   deriving (eq, show)  instance arbitrary  => arbitrary (list a)   arbitrary =     <- arbitrary     a' <- arbitrary     a'' <- arbitrary     return $ (cons (cons a' (cons a'' (nil)))) 

with sized. enables manage size of generated arbitrary, although semantics instance:

instance arbitrary => arbitrary (list a)    arbitrary = sized go     go 0 = pure nil           go n =             xs <- go (n - 1)             x  <- arbitrary             return (cons x xs) 

for comparison, here []'s arbitrary instance:

instance arbitrary => arbitrary [a]   arbitrary = sized $ \n ->     k <- choose (0,n)        sequence [ arbitrary | _ <- [1..k] ] 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

python - pip wont install .WHL files -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -