Ocaml - tree preorder/postorder/inorder












0















I have that definition of trees given in OCaml



type 'a tree = Node of 'a * 'a tree list;;

let rec fold_tree f (Node (x,l)) =
f x (map (fold_tree f) l);;


Can somebody help me how can I write for example preorder in use of fold_tree (without additional recursion). I know how I can do this without fold_tree but this makes me problem



So far I have got this:



let preorder t =
fold_tree (fun x l ->
(fold_left(fun acc h -> h@acc) x l ) ) t;;


but ocaml consider t as tree list...










share|improve this question




















  • 2





    What have you written so far, and why does it not seem to be working?

    – Jeffrey Scofield
    Nov 12 '18 at 20:56











  • @JeffreyScofield yes, you have right, I mentioned problem but I did not say anything about my trying. I edited a post.

    – mvxxx
    Nov 12 '18 at 21:31






  • 1





    Did you not take anything from the answer to your last question? If you follow those advices, what happens then?

    – glennsl
    Nov 12 '18 at 21:33













  • ignoring open and hd does not matter there. But if it comes to another advices, or I didnt understand them or there does not matter too in that situation

    – mvxxx
    Nov 12 '18 at 21:45











  • You don't think type annotations matter for understanding the cause of a type error?

    – glennsl
    Nov 12 '18 at 21:56


















0















I have that definition of trees given in OCaml



type 'a tree = Node of 'a * 'a tree list;;

let rec fold_tree f (Node (x,l)) =
f x (map (fold_tree f) l);;


Can somebody help me how can I write for example preorder in use of fold_tree (without additional recursion). I know how I can do this without fold_tree but this makes me problem



So far I have got this:



let preorder t =
fold_tree (fun x l ->
(fold_left(fun acc h -> h@acc) x l ) ) t;;


but ocaml consider t as tree list...










share|improve this question




















  • 2





    What have you written so far, and why does it not seem to be working?

    – Jeffrey Scofield
    Nov 12 '18 at 20:56











  • @JeffreyScofield yes, you have right, I mentioned problem but I did not say anything about my trying. I edited a post.

    – mvxxx
    Nov 12 '18 at 21:31






  • 1





    Did you not take anything from the answer to your last question? If you follow those advices, what happens then?

    – glennsl
    Nov 12 '18 at 21:33













  • ignoring open and hd does not matter there. But if it comes to another advices, or I didnt understand them or there does not matter too in that situation

    – mvxxx
    Nov 12 '18 at 21:45











  • You don't think type annotations matter for understanding the cause of a type error?

    – glennsl
    Nov 12 '18 at 21:56
















0












0








0








I have that definition of trees given in OCaml



type 'a tree = Node of 'a * 'a tree list;;

let rec fold_tree f (Node (x,l)) =
f x (map (fold_tree f) l);;


Can somebody help me how can I write for example preorder in use of fold_tree (without additional recursion). I know how I can do this without fold_tree but this makes me problem



So far I have got this:



let preorder t =
fold_tree (fun x l ->
(fold_left(fun acc h -> h@acc) x l ) ) t;;


but ocaml consider t as tree list...










share|improve this question
















I have that definition of trees given in OCaml



type 'a tree = Node of 'a * 'a tree list;;

let rec fold_tree f (Node (x,l)) =
f x (map (fold_tree f) l);;


Can somebody help me how can I write for example preorder in use of fold_tree (without additional recursion). I know how I can do this without fold_tree but this makes me problem



So far I have got this:



let preorder t =
fold_tree (fun x l ->
(fold_left(fun acc h -> h@acc) x l ) ) t;;


but ocaml consider t as tree list...







tree ocaml preorder






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 21:30







mvxxx

















asked Nov 12 '18 at 20:40









mvxxxmvxxx

556




556








  • 2





    What have you written so far, and why does it not seem to be working?

    – Jeffrey Scofield
    Nov 12 '18 at 20:56











  • @JeffreyScofield yes, you have right, I mentioned problem but I did not say anything about my trying. I edited a post.

    – mvxxx
    Nov 12 '18 at 21:31






  • 1





    Did you not take anything from the answer to your last question? If you follow those advices, what happens then?

    – glennsl
    Nov 12 '18 at 21:33













  • ignoring open and hd does not matter there. But if it comes to another advices, or I didnt understand them or there does not matter too in that situation

    – mvxxx
    Nov 12 '18 at 21:45











  • You don't think type annotations matter for understanding the cause of a type error?

    – glennsl
    Nov 12 '18 at 21:56
















  • 2





    What have you written so far, and why does it not seem to be working?

    – Jeffrey Scofield
    Nov 12 '18 at 20:56











  • @JeffreyScofield yes, you have right, I mentioned problem but I did not say anything about my trying. I edited a post.

    – mvxxx
    Nov 12 '18 at 21:31






  • 1





    Did you not take anything from the answer to your last question? If you follow those advices, what happens then?

    – glennsl
    Nov 12 '18 at 21:33













  • ignoring open and hd does not matter there. But if it comes to another advices, or I didnt understand them or there does not matter too in that situation

    – mvxxx
    Nov 12 '18 at 21:45











  • You don't think type annotations matter for understanding the cause of a type error?

    – glennsl
    Nov 12 '18 at 21:56










2




2





What have you written so far, and why does it not seem to be working?

– Jeffrey Scofield
Nov 12 '18 at 20:56





What have you written so far, and why does it not seem to be working?

– Jeffrey Scofield
Nov 12 '18 at 20:56













@JeffreyScofield yes, you have right, I mentioned problem but I did not say anything about my trying. I edited a post.

– mvxxx
Nov 12 '18 at 21:31





@JeffreyScofield yes, you have right, I mentioned problem but I did not say anything about my trying. I edited a post.

– mvxxx
Nov 12 '18 at 21:31




1




1





Did you not take anything from the answer to your last question? If you follow those advices, what happens then?

– glennsl
Nov 12 '18 at 21:33







Did you not take anything from the answer to your last question? If you follow those advices, what happens then?

– glennsl
Nov 12 '18 at 21:33















ignoring open and hd does not matter there. But if it comes to another advices, or I didnt understand them or there does not matter too in that situation

– mvxxx
Nov 12 '18 at 21:45





ignoring open and hd does not matter there. But if it comes to another advices, or I didnt understand them or there does not matter too in that situation

– mvxxx
Nov 12 '18 at 21:45













You don't think type annotations matter for understanding the cause of a type error?

– glennsl
Nov 12 '18 at 21:56







You don't think type annotations matter for understanding the cause of a type error?

– glennsl
Nov 12 '18 at 21:56














1 Answer
1






active

oldest

votes


















0














    OCaml version 4.02.3

# type 'a tree = Node of 'a * 'a tree list;;
type 'a tree = Node of 'a * 'a tree list
# let rec fold_tree f (Node (x,l)) =
f x (List.map (fold_tree f) l);;
val fold_tree : ('a -> 'b list -> 'b) -> 'a tree -> 'b = <fun>
# let preorder t =
fold_tree (fun x l ->
(List.fold_left(fun acc h -> h@acc) x l ) ) t;;
val preorder : 'a list tree -> 'a list = <fun>


You are using x as start value for the List.fold_left and append values to it. That makes x a 'a list and therefore t must be 'a list tree. Change x to [x] and you get:



# let preorder t =
fold_tree (fun x l ->
(List.fold_left(fun acc h -> h@acc) [x] l ) ) t;;
val preorder : 'a tree -> 'a list = <fun>





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269771%2focaml-tree-preorder-postorder-inorder%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














        OCaml version 4.02.3

    # type 'a tree = Node of 'a * 'a tree list;;
    type 'a tree = Node of 'a * 'a tree list
    # let rec fold_tree f (Node (x,l)) =
    f x (List.map (fold_tree f) l);;
    val fold_tree : ('a -> 'b list -> 'b) -> 'a tree -> 'b = <fun>
    # let preorder t =
    fold_tree (fun x l ->
    (List.fold_left(fun acc h -> h@acc) x l ) ) t;;
    val preorder : 'a list tree -> 'a list = <fun>


    You are using x as start value for the List.fold_left and append values to it. That makes x a 'a list and therefore t must be 'a list tree. Change x to [x] and you get:



    # let preorder t =
    fold_tree (fun x l ->
    (List.fold_left(fun acc h -> h@acc) [x] l ) ) t;;
    val preorder : 'a tree -> 'a list = <fun>





    share|improve this answer




























      0














          OCaml version 4.02.3

      # type 'a tree = Node of 'a * 'a tree list;;
      type 'a tree = Node of 'a * 'a tree list
      # let rec fold_tree f (Node (x,l)) =
      f x (List.map (fold_tree f) l);;
      val fold_tree : ('a -> 'b list -> 'b) -> 'a tree -> 'b = <fun>
      # let preorder t =
      fold_tree (fun x l ->
      (List.fold_left(fun acc h -> h@acc) x l ) ) t;;
      val preorder : 'a list tree -> 'a list = <fun>


      You are using x as start value for the List.fold_left and append values to it. That makes x a 'a list and therefore t must be 'a list tree. Change x to [x] and you get:



      # let preorder t =
      fold_tree (fun x l ->
      (List.fold_left(fun acc h -> h@acc) [x] l ) ) t;;
      val preorder : 'a tree -> 'a list = <fun>





      share|improve this answer


























        0












        0








        0







            OCaml version 4.02.3

        # type 'a tree = Node of 'a * 'a tree list;;
        type 'a tree = Node of 'a * 'a tree list
        # let rec fold_tree f (Node (x,l)) =
        f x (List.map (fold_tree f) l);;
        val fold_tree : ('a -> 'b list -> 'b) -> 'a tree -> 'b = <fun>
        # let preorder t =
        fold_tree (fun x l ->
        (List.fold_left(fun acc h -> h@acc) x l ) ) t;;
        val preorder : 'a list tree -> 'a list = <fun>


        You are using x as start value for the List.fold_left and append values to it. That makes x a 'a list and therefore t must be 'a list tree. Change x to [x] and you get:



        # let preorder t =
        fold_tree (fun x l ->
        (List.fold_left(fun acc h -> h@acc) [x] l ) ) t;;
        val preorder : 'a tree -> 'a list = <fun>





        share|improve this answer













            OCaml version 4.02.3

        # type 'a tree = Node of 'a * 'a tree list;;
        type 'a tree = Node of 'a * 'a tree list
        # let rec fold_tree f (Node (x,l)) =
        f x (List.map (fold_tree f) l);;
        val fold_tree : ('a -> 'b list -> 'b) -> 'a tree -> 'b = <fun>
        # let preorder t =
        fold_tree (fun x l ->
        (List.fold_left(fun acc h -> h@acc) x l ) ) t;;
        val preorder : 'a list tree -> 'a list = <fun>


        You are using x as start value for the List.fold_left and append values to it. That makes x a 'a list and therefore t must be 'a list tree. Change x to [x] and you get:



        # let preorder t =
        fold_tree (fun x l ->
        (List.fold_left(fun acc h -> h@acc) [x] l ) ) t;;
        val preorder : 'a tree -> 'a list = <fun>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 13:58









        Goswin von BrederlowGoswin von Brederlow

        2,817724




        2,817724






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269771%2focaml-tree-preorder-postorder-inorder%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Full-time equivalent

            さくらももこ

            13 indicted, 8 arrested in Calif. drug cartel investigation