short-circuit like Maybe monad with a difference
I have a function 'evalRule' that applies rules to steps in a backtracking search optimization problem and evaluates the step's fitness or decides that it must be eliminated entirely. In the following, a result of Nothing means "eliminate the step," Just x means the step has fitness score x. evalRule :: Rule -> Step -> Maybe Double I would like to write a function that applies a bunch of rules but short-circuits the computation if it hits Nothing. However, unlike the way the Maybe monad works, I want to know the partial results. In the following, the rules are applied in order and the "Just" results are collected up the point where a rule returns Nothing. evalRules :: [Rule] -> Step -> [Double] What's a nice way to do this? D
short-circuits the computation if it hits Nothing. However, unlike the way the Maybe monad works, I want to know the partial results. could store partial results in a State monad.. Maybe would short circuit but the values would stay in the State.
would this work?
I just tried something evalRules :: [Rule] -> Step -> [Double] evalRules rules step = catMaybes . takeWhile isJust . map (flip evalRule step) $ rules This seems to work according to my testing. On Fri, Sep 30, 2016 at 1:32 PM, Imants Cekusins <imantc@gmail.com> wrote:
short-circuits the computation if it hits Nothing. However, unlike the way the Maybe monad works, I want to know the partial results. could store partial results in a State monad.. Maybe would short circuit but the values would stay in the State.
would this work?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
it is better alright. is it necessary to know if all rules were evaluated?
It is necessary to know if all the rules are evaluated but that can be done by checking the length of the output. Also I could do something like zip the rule list with the [Double] output to tuple the rules together with their scores. D On Fri, Sep 30, 2016 at 2:12 PM, Imants Cekusins <imantc@gmail.com> wrote:
it is better alright.
is it necessary to know if all rules were evaluated?
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (2)
-
Dennis Raddle -
Imants Cekusins