I am trying to create some rules in a tree structure with logical gates, that is, not, or as conditions, for example. property x is equal to the value of y. First I wrote the most obvious recursive function that worked. Then I tried to write a version that would not cause a stack overflow in the continuation of the style, taking my cue from the post of the general bending wood and qaru.site/questions/115694 / ... .
It works for small trees (depth about 1000), but unfortunately, when using a large tree, it calls stackoverflow when I run it on my Mac with Xamarin Studio. Can someone tell me if I misunderstood how F # handles tail recursive code or is this code not tail recursive?
Full sample here .
let FoldTree andF orF notF leafV t data =
let rec Loop t cont =
match t with
| AndGate (left, right)->
Loop left (fun lacc ->
Loop right (fun racc ->
cont (andF lacc racc)))
| OrGate (left, right)->
Loop left (fun lacc ->
Loop right (fun racc ->
cont (orF lacc racc)))
| NotGate exp ->
Loop exp (fun acc -> cont (notF acc))
| EqualsExpression(property,value) -> cont (leafV (property,value))
Loop t id
let evaluateContinuationPassingStyle tree data =
FoldTree (&&) (||) (not) (fun (prop,value) -> data |> Map.find prop |> ((=) value)) tree data
Nickl source
share