F # tree function causes stack overflow in Xamarin Studio

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
+4
source share
1 answer

The tail-recursive code, you got it right. But the problem is Mono. Look, Mono is not such a high-quality implementation of .NET as an official thing. In particular, this is not eliminating the tail call. Like, in general.

( ) , . F # , , , , , while, .

, , , . .

:

  • .NET Core.
  • , (, ).
  • .
  • , .
+6

Source: https://habr.com/ru/post/1670180/


All Articles