F#: integers to pair of integers











up vote
0
down vote

favorite












I have function that takes a list of integers as an argument and returns a list of pairs as a result. For example the [1;2;3;4] should be returned as [(1, 2); (3, 4)]



I have implemented the below function for this.



let listToPairList (list: int list) = 
let index,pairList = List.foldBack(fun elem (iAcc,listAcc) ->
if (iAcc % 2 = 0) then
(iAcc - 1,(elem,list.[iAcc + 1])::listAcc)
else
(iAcc - 1,listAcc)) list (list.Length - 1, )
pairList


Now ,I want to do it with using foldBack function but without using indexes. Could anyone give me an idea about how to make it ?



Any help would be appreciated.










share|improve this question
























  • What do you want to do if the list contains an odd number of elements?
    – Lee
    Nov 8 at 12:21










  • Last element makes a pair with itself
    – Tartar
    Nov 8 at 12:38















up vote
0
down vote

favorite












I have function that takes a list of integers as an argument and returns a list of pairs as a result. For example the [1;2;3;4] should be returned as [(1, 2); (3, 4)]



I have implemented the below function for this.



let listToPairList (list: int list) = 
let index,pairList = List.foldBack(fun elem (iAcc,listAcc) ->
if (iAcc % 2 = 0) then
(iAcc - 1,(elem,list.[iAcc + 1])::listAcc)
else
(iAcc - 1,listAcc)) list (list.Length - 1, )
pairList


Now ,I want to do it with using foldBack function but without using indexes. Could anyone give me an idea about how to make it ?



Any help would be appreciated.










share|improve this question
























  • What do you want to do if the list contains an odd number of elements?
    – Lee
    Nov 8 at 12:21










  • Last element makes a pair with itself
    – Tartar
    Nov 8 at 12:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have function that takes a list of integers as an argument and returns a list of pairs as a result. For example the [1;2;3;4] should be returned as [(1, 2); (3, 4)]



I have implemented the below function for this.



let listToPairList (list: int list) = 
let index,pairList = List.foldBack(fun elem (iAcc,listAcc) ->
if (iAcc % 2 = 0) then
(iAcc - 1,(elem,list.[iAcc + 1])::listAcc)
else
(iAcc - 1,listAcc)) list (list.Length - 1, )
pairList


Now ,I want to do it with using foldBack function but without using indexes. Could anyone give me an idea about how to make it ?



Any help would be appreciated.










share|improve this question















I have function that takes a list of integers as an argument and returns a list of pairs as a result. For example the [1;2;3;4] should be returned as [(1, 2); (3, 4)]



I have implemented the below function for this.



let listToPairList (list: int list) = 
let index,pairList = List.foldBack(fun elem (iAcc,listAcc) ->
if (iAcc % 2 = 0) then
(iAcc - 1,(elem,list.[iAcc + 1])::listAcc)
else
(iAcc - 1,listAcc)) list (list.Length - 1, )
pairList


Now ,I want to do it with using foldBack function but without using indexes. Could anyone give me an idea about how to make it ?



Any help would be appreciated.







functional-programming f#






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 14:12









Tomas Petricek

196k13284457




196k13284457










asked Nov 8 at 12:05









Tartar

1,31262953




1,31262953












  • What do you want to do if the list contains an odd number of elements?
    – Lee
    Nov 8 at 12:21










  • Last element makes a pair with itself
    – Tartar
    Nov 8 at 12:38


















  • What do you want to do if the list contains an odd number of elements?
    – Lee
    Nov 8 at 12:21










  • Last element makes a pair with itself
    – Tartar
    Nov 8 at 12:38
















What do you want to do if the list contains an odd number of elements?
– Lee
Nov 8 at 12:21




What do you want to do if the list contains an odd number of elements?
– Lee
Nov 8 at 12:21












Last element makes a pair with itself
– Tartar
Nov 8 at 12:38




Last element makes a pair with itself
– Tartar
Nov 8 at 12:38












2 Answers
2






active

oldest

votes

















up vote
3
down vote













Why using foldback?



What about a simple recursive function



let rec listToPairList = function
| ->
| x:: -> [(x,x)]
| x::y::xs -> (x,y)::listToPairList xs


Or a tail recursive one:



let listToPairList lst =
let rec aux acc = function
| -> acc |> List.rev
| x:: -> (x,x)::acc |> List.rev
| x1::x2::xs -> aux ((x1,x2)::acc) xs
aux lst





share|improve this answer






























    up vote
    1
    down vote













    You could use an int option to track the next item in through the fold:



    let listToPairList (list: int list) = 
    let (_, pairList) = List.foldBack (fun elem (pairAcc, listAcc) ->
    match pairAcc with
    | None -> (Some(elem), listAcc)
    | Some(next) -> (None, (elem, next) :: listAcc))
    list
    (None, )
    pairList


    Be aware this will drop the first item in the list if the input contains an odd number of elements.






    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',
      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%2f53207395%2ff-integers-to-pair-of-integers%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








      up vote
      3
      down vote













      Why using foldback?



      What about a simple recursive function



      let rec listToPairList = function
      | ->
      | x:: -> [(x,x)]
      | x::y::xs -> (x,y)::listToPairList xs


      Or a tail recursive one:



      let listToPairList lst =
      let rec aux acc = function
      | -> acc |> List.rev
      | x:: -> (x,x)::acc |> List.rev
      | x1::x2::xs -> aux ((x1,x2)::acc) xs
      aux lst





      share|improve this answer



























        up vote
        3
        down vote













        Why using foldback?



        What about a simple recursive function



        let rec listToPairList = function
        | ->
        | x:: -> [(x,x)]
        | x::y::xs -> (x,y)::listToPairList xs


        Or a tail recursive one:



        let listToPairList lst =
        let rec aux acc = function
        | -> acc |> List.rev
        | x:: -> (x,x)::acc |> List.rev
        | x1::x2::xs -> aux ((x1,x2)::acc) xs
        aux lst





        share|improve this answer

























          up vote
          3
          down vote










          up vote
          3
          down vote









          Why using foldback?



          What about a simple recursive function



          let rec listToPairList = function
          | ->
          | x:: -> [(x,x)]
          | x::y::xs -> (x,y)::listToPairList xs


          Or a tail recursive one:



          let listToPairList lst =
          let rec aux acc = function
          | -> acc |> List.rev
          | x:: -> (x,x)::acc |> List.rev
          | x1::x2::xs -> aux ((x1,x2)::acc) xs
          aux lst





          share|improve this answer














          Why using foldback?



          What about a simple recursive function



          let rec listToPairList = function
          | ->
          | x:: -> [(x,x)]
          | x::y::xs -> (x,y)::listToPairList xs


          Or a tail recursive one:



          let listToPairList lst =
          let rec aux acc = function
          | -> acc |> List.rev
          | x:: -> (x,x)::acc |> List.rev
          | x1::x2::xs -> aux ((x1,x2)::acc) xs
          aux lst






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 11 at 8:08

























          answered Nov 8 at 12:59









          gileCAD

          1,28656




          1,28656
























              up vote
              1
              down vote













              You could use an int option to track the next item in through the fold:



              let listToPairList (list: int list) = 
              let (_, pairList) = List.foldBack (fun elem (pairAcc, listAcc) ->
              match pairAcc with
              | None -> (Some(elem), listAcc)
              | Some(next) -> (None, (elem, next) :: listAcc))
              list
              (None, )
              pairList


              Be aware this will drop the first item in the list if the input contains an odd number of elements.






              share|improve this answer

























                up vote
                1
                down vote













                You could use an int option to track the next item in through the fold:



                let listToPairList (list: int list) = 
                let (_, pairList) = List.foldBack (fun elem (pairAcc, listAcc) ->
                match pairAcc with
                | None -> (Some(elem), listAcc)
                | Some(next) -> (None, (elem, next) :: listAcc))
                list
                (None, )
                pairList


                Be aware this will drop the first item in the list if the input contains an odd number of elements.






                share|improve this answer























                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  You could use an int option to track the next item in through the fold:



                  let listToPairList (list: int list) = 
                  let (_, pairList) = List.foldBack (fun elem (pairAcc, listAcc) ->
                  match pairAcc with
                  | None -> (Some(elem), listAcc)
                  | Some(next) -> (None, (elem, next) :: listAcc))
                  list
                  (None, )
                  pairList


                  Be aware this will drop the first item in the list if the input contains an odd number of elements.






                  share|improve this answer












                  You could use an int option to track the next item in through the fold:



                  let listToPairList (list: int list) = 
                  let (_, pairList) = List.foldBack (fun elem (pairAcc, listAcc) ->
                  match pairAcc with
                  | None -> (Some(elem), listAcc)
                  | Some(next) -> (None, (elem, next) :: listAcc))
                  list
                  (None, )
                  pairList


                  Be aware this will drop the first item in the list if the input contains an odd number of elements.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 8 at 12:23









                  Lee

                  118k14173239




                  118k14173239






























                       

                      draft saved


                      draft discarded



















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53207395%2ff-integers-to-pair-of-integers%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

                      How to pass form data using jquery Ajax to insert data in database?

                      National Museum of Racing and Hall of Fame

                      Guess what letter conforming each word