How do I make the function return a float list?










0














Please, how do I make this function return the value of every branch and leaf as a float list? I have tried several methods with Tail recursion but I am not able to return the head I cannot loop through the branch and leaf.



type 'a Tree = | Leaf of 'a | Branch of 'a Tree * 'a Tree

let medianInTree (lst: float Tree) :float list=
let rec medianInTree' (a : float Tree) acc =
match lst with
| Leaf(n) -> n :: acc
| Branch(Leaf(xx), Leaf(xs)) -> xx :: [xs]
| Branch(Leaf(x), Branch(Leaf(xx), Leaf(xs))) ->
let acc = medianInTree'(Leaf(x)) acc
medianInTree' (Branch(Leaf(xx), Leaf(xs))) acc
| Branch(_, _) ->
medianInTree' lst


Question: medianInTree (Branch(Leaf(2.0), Branch(Leaf(3.0), Leaf(5.0))))



I want this result: [2.0;3.0;5.0]










share|improve this question




























    0














    Please, how do I make this function return the value of every branch and leaf as a float list? I have tried several methods with Tail recursion but I am not able to return the head I cannot loop through the branch and leaf.



    type 'a Tree = | Leaf of 'a | Branch of 'a Tree * 'a Tree

    let medianInTree (lst: float Tree) :float list=
    let rec medianInTree' (a : float Tree) acc =
    match lst with
    | Leaf(n) -> n :: acc
    | Branch(Leaf(xx), Leaf(xs)) -> xx :: [xs]
    | Branch(Leaf(x), Branch(Leaf(xx), Leaf(xs))) ->
    let acc = medianInTree'(Leaf(x)) acc
    medianInTree' (Branch(Leaf(xx), Leaf(xs))) acc
    | Branch(_, _) ->
    medianInTree' lst


    Question: medianInTree (Branch(Leaf(2.0), Branch(Leaf(3.0), Leaf(5.0))))



    I want this result: [2.0;3.0;5.0]










    share|improve this question


























      0












      0








      0







      Please, how do I make this function return the value of every branch and leaf as a float list? I have tried several methods with Tail recursion but I am not able to return the head I cannot loop through the branch and leaf.



      type 'a Tree = | Leaf of 'a | Branch of 'a Tree * 'a Tree

      let medianInTree (lst: float Tree) :float list=
      let rec medianInTree' (a : float Tree) acc =
      match lst with
      | Leaf(n) -> n :: acc
      | Branch(Leaf(xx), Leaf(xs)) -> xx :: [xs]
      | Branch(Leaf(x), Branch(Leaf(xx), Leaf(xs))) ->
      let acc = medianInTree'(Leaf(x)) acc
      medianInTree' (Branch(Leaf(xx), Leaf(xs))) acc
      | Branch(_, _) ->
      medianInTree' lst


      Question: medianInTree (Branch(Leaf(2.0), Branch(Leaf(3.0), Leaf(5.0))))



      I want this result: [2.0;3.0;5.0]










      share|improve this question















      Please, how do I make this function return the value of every branch and leaf as a float list? I have tried several methods with Tail recursion but I am not able to return the head I cannot loop through the branch and leaf.



      type 'a Tree = | Leaf of 'a | Branch of 'a Tree * 'a Tree

      let medianInTree (lst: float Tree) :float list=
      let rec medianInTree' (a : float Tree) acc =
      match lst with
      | Leaf(n) -> n :: acc
      | Branch(Leaf(xx), Leaf(xs)) -> xx :: [xs]
      | Branch(Leaf(x), Branch(Leaf(xx), Leaf(xs))) ->
      let acc = medianInTree'(Leaf(x)) acc
      medianInTree' (Branch(Leaf(xx), Leaf(xs))) acc
      | Branch(_, _) ->
      medianInTree' lst


      Question: medianInTree (Branch(Leaf(2.0), Branch(Leaf(3.0), Leaf(5.0))))



      I want this result: [2.0;3.0;5.0]







      f# f#-interactive f#-data f#-3.0 f#-fake






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 16:18









      psfinaki

      368214




      368214










      asked Nov 11 at 15:33









      T.M

      389




      389






















          2 Answers
          2






          active

          oldest

          votes


















          2














          using an accumulator, you can do something like this:



          let flatten tree =
          let rec toList tree acc =
          match tree with
          | Leaf a -> a :: acc
          | Branch(left, right) ->
          let acc = toList left acc
          toList right acc
          toList tree |> List.rev


          But doing so, the recursive call to process the left branch is not tail recursive.
          To insure tail recursion while processing tree structures, you have to use continuations.



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch(left, right) -> toList left (fun l ->
          toList right (fun r ->
          cont r) (cont l)) acc
          toList tree id |> List.rev


          Which can be simplified as:



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch (left, right) -> toList left (toList right cont) acc
          toList tree id |> List.rev





          share|improve this answer






















          • Thanks a lot gileCAD
            – T.M
            Nov 11 at 16:21


















          1














          Your main bug is using match with lst instead of on a. I made it a bit simpler as well.



          let medianInTree (lst: float Tree) :float list=
          let rec medianInTree' (a : float Tree)=
          match a with
          | Leaf(n) -> [n]
          | Branch(l, r) -> (medianInTree' l) @ (medianInTree' r)
          medianInTree' lst





          share|improve this answer






















          • Thank you Ringil
            – T.M
            Nov 11 at 16:21










          • @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
            – gileCAD
            Nov 11 at 17:02











          • @gileCAD you're right. thanks for pointing that out.
            – Ringil
            Nov 11 at 17:36










          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%2f53250261%2fhow-do-i-make-the-function-return-a-float-list%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          using an accumulator, you can do something like this:



          let flatten tree =
          let rec toList tree acc =
          match tree with
          | Leaf a -> a :: acc
          | Branch(left, right) ->
          let acc = toList left acc
          toList right acc
          toList tree |> List.rev


          But doing so, the recursive call to process the left branch is not tail recursive.
          To insure tail recursion while processing tree structures, you have to use continuations.



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch(left, right) -> toList left (fun l ->
          toList right (fun r ->
          cont r) (cont l)) acc
          toList tree id |> List.rev


          Which can be simplified as:



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch (left, right) -> toList left (toList right cont) acc
          toList tree id |> List.rev





          share|improve this answer






















          • Thanks a lot gileCAD
            – T.M
            Nov 11 at 16:21















          2














          using an accumulator, you can do something like this:



          let flatten tree =
          let rec toList tree acc =
          match tree with
          | Leaf a -> a :: acc
          | Branch(left, right) ->
          let acc = toList left acc
          toList right acc
          toList tree |> List.rev


          But doing so, the recursive call to process the left branch is not tail recursive.
          To insure tail recursion while processing tree structures, you have to use continuations.



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch(left, right) -> toList left (fun l ->
          toList right (fun r ->
          cont r) (cont l)) acc
          toList tree id |> List.rev


          Which can be simplified as:



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch (left, right) -> toList left (toList right cont) acc
          toList tree id |> List.rev





          share|improve this answer






















          • Thanks a lot gileCAD
            – T.M
            Nov 11 at 16:21













          2












          2








          2






          using an accumulator, you can do something like this:



          let flatten tree =
          let rec toList tree acc =
          match tree with
          | Leaf a -> a :: acc
          | Branch(left, right) ->
          let acc = toList left acc
          toList right acc
          toList tree |> List.rev


          But doing so, the recursive call to process the left branch is not tail recursive.
          To insure tail recursion while processing tree structures, you have to use continuations.



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch(left, right) -> toList left (fun l ->
          toList right (fun r ->
          cont r) (cont l)) acc
          toList tree id |> List.rev


          Which can be simplified as:



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch (left, right) -> toList left (toList right cont) acc
          toList tree id |> List.rev





          share|improve this answer














          using an accumulator, you can do something like this:



          let flatten tree =
          let rec toList tree acc =
          match tree with
          | Leaf a -> a :: acc
          | Branch(left, right) ->
          let acc = toList left acc
          toList right acc
          toList tree |> List.rev


          But doing so, the recursive call to process the left branch is not tail recursive.
          To insure tail recursion while processing tree structures, you have to use continuations.



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch(left, right) -> toList left (fun l ->
          toList right (fun r ->
          cont r) (cont l)) acc
          toList tree id |> List.rev


          Which can be simplified as:



          let flatten tree = 
          let rec toList tree cont acc =
          match tree with
          | Leaf a -> cont (a :: acc)
          | Branch (left, right) -> toList left (toList right cont) acc
          toList tree id |> List.rev






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 16:18

























          answered Nov 11 at 16:13









          gileCAD

          1,31256




          1,31256











          • Thanks a lot gileCAD
            – T.M
            Nov 11 at 16:21
















          • Thanks a lot gileCAD
            – T.M
            Nov 11 at 16:21















          Thanks a lot gileCAD
          – T.M
          Nov 11 at 16:21




          Thanks a lot gileCAD
          – T.M
          Nov 11 at 16:21













          1














          Your main bug is using match with lst instead of on a. I made it a bit simpler as well.



          let medianInTree (lst: float Tree) :float list=
          let rec medianInTree' (a : float Tree)=
          match a with
          | Leaf(n) -> [n]
          | Branch(l, r) -> (medianInTree' l) @ (medianInTree' r)
          medianInTree' lst





          share|improve this answer






















          • Thank you Ringil
            – T.M
            Nov 11 at 16:21










          • @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
            – gileCAD
            Nov 11 at 17:02











          • @gileCAD you're right. thanks for pointing that out.
            – Ringil
            Nov 11 at 17:36















          1














          Your main bug is using match with lst instead of on a. I made it a bit simpler as well.



          let medianInTree (lst: float Tree) :float list=
          let rec medianInTree' (a : float Tree)=
          match a with
          | Leaf(n) -> [n]
          | Branch(l, r) -> (medianInTree' l) @ (medianInTree' r)
          medianInTree' lst





          share|improve this answer






















          • Thank you Ringil
            – T.M
            Nov 11 at 16:21










          • @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
            – gileCAD
            Nov 11 at 17:02











          • @gileCAD you're right. thanks for pointing that out.
            – Ringil
            Nov 11 at 17:36













          1












          1








          1






          Your main bug is using match with lst instead of on a. I made it a bit simpler as well.



          let medianInTree (lst: float Tree) :float list=
          let rec medianInTree' (a : float Tree)=
          match a with
          | Leaf(n) -> [n]
          | Branch(l, r) -> (medianInTree' l) @ (medianInTree' r)
          medianInTree' lst





          share|improve this answer














          Your main bug is using match with lst instead of on a. I made it a bit simpler as well.



          let medianInTree (lst: float Tree) :float list=
          let rec medianInTree' (a : float Tree)=
          match a with
          | Leaf(n) -> [n]
          | Branch(l, r) -> (medianInTree' l) @ (medianInTree' r)
          medianInTree' lst






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 17:36

























          answered Nov 11 at 16:07









          Ringil

          3,44021025




          3,44021025











          • Thank you Ringil
            – T.M
            Nov 11 at 16:21










          • @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
            – gileCAD
            Nov 11 at 17:02











          • @gileCAD you're right. thanks for pointing that out.
            – Ringil
            Nov 11 at 17:36
















          • Thank you Ringil
            – T.M
            Nov 11 at 16:21










          • @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
            – gileCAD
            Nov 11 at 17:02











          • @gileCAD you're right. thanks for pointing that out.
            – Ringil
            Nov 11 at 17:36















          Thank you Ringil
          – T.M
          Nov 11 at 16:21




          Thank you Ringil
          – T.M
          Nov 11 at 16:21












          @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
          – gileCAD
          Nov 11 at 17:02





          @Ringil while using concat, you do not need an accumulator: let rec toList = function Leaf a -> [a] | Branch (l, r) -> toList l @ toList r
          – gileCAD
          Nov 11 at 17:02













          @gileCAD you're right. thanks for pointing that out.
          – Ringil
          Nov 11 at 17:36




          @gileCAD you're right. thanks for pointing that out.
          – Ringil
          Nov 11 at 17:36

















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53250261%2fhow-do-i-make-the-function-return-a-float-list%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

          Use pre created SQLite database for Android project in kotlin

          Darth Vader #20

          Ondo