in Education by
For a given predicate pred : 'a list -> bool and generator gen : Gen<'a>, consider the following generator of well-formed lists that satisfy the predicate: let wellFormedList pred gen = Gen.ofList gen |> Gen.filter pred As mentioned in the FsCheck manual, there should be a high chance that predicate holds for a random list. Unfortunately, this assumption does not hold in my case. Thus, I need te define a custom generator for lists that satisfy the predicate. How can I define a custom generator that starts from the empty list and extends it with new random elements, until the list satisfies the predicate? I probably need to use the computation expression gen { } for generators, but I do not see how. PS: I am aware that, unlike the original implementation of wellFormedList, the distribution of such a custom generator is not uniform over all lists that satisfy the predicate. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Well if I'm understanding it right, I think this should do it: let wellFormedList (predicate:'a list -> bool) (myGen:Gen<'a>) = gen { let! initialList = Gen.listOf myGen let mutable myList = initialList while not <| predicate myList do let! newVal = myGen myList <- (newVal :: myList) return myList } or if you wanted a recursive function: let wellFormedList (predicate:'a list -> bool) (myGen:Gen<'a>) = gen { let rec addIfNecessary listSoFar = if predicate listSoFar then gen { return listSoFar } else gen { let! newVal = myGen return! addIfNecessary (newVal :: listSoFar) } let! initialList = Gen.listOf myGen return! addIfNecessary initialList } I haven't checked what kind of distributions that gives you though. And of course you risk an infinite loop (or stack overflow) if the list never converges on a form that passes the predicate.

Related questions

0 votes
    I have 2 Tables, Receipts which contains columns like Id, InvoiceId etc and Invoices, which Contains Id, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 10, 2022 in Education by JackTerrance
0 votes
    I have 2 Tables, Receipts which contains columns like Id, InvoiceId etc and Invoices, which Contains Id, ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 7, 2022 in Education by JackTerrance
0 votes
    The following script I have saved in an .sh file on the server that clears a few directories of ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    sentence contains or implies a predicate and a subject. Myth Fact Select the correct answer from above options...
asked Dec 25, 2021 in Education by JackTerrance
0 votes
    The predicate in a where clause can involve Boolean operations such as and. The result of true and ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    The ______ clause allows us to select only those rows in the result relation of the ____ clause that ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    _______________ allows a system administrator to associate a function with a relation; the function returns a ... Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What is the purpose of Predicate functional interface in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    How do I generate 3 Gaussian variables? I know that the Box-Muller algorithm can be used to convert ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 7, 2022 in Education by JackTerrance
0 votes
    I have an PHP SDK for Twitter OAuth, but in it's constructor, I have to pass the OAuth secret ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    The resistance of the primary coil of a well-designed, 1: 10 step-down transfomer is 12. With the secondary ... in the primary coil Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
0 votes
    Why is Talend called a Code Generator?...
asked Mar 18, 2021 in Technology by JackTerrance
0 votes
    What is Code Generator in Talend?...
asked Mar 16, 2021 in Technology by JackTerrance
...