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.
functional-programming f#
add a comment |
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.
functional-programming f#
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
add a comment |
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.
functional-programming f#
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#
functional-programming f#
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
add a comment |
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
add a comment |
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
add a comment |
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.
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 11 at 8:08
answered Nov 8 at 12:59
gileCAD
1,28656
1,28656
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
answered Nov 8 at 12:23
Lee
118k14173239
118k14173239
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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