F# making list of pairs from two lists
I need to do create a function in F# to pair elements in two lists together(See (ii) in the following picture) :
I'm actually not completely sure what the question is asking: Do I need to create a function that takes two lists and return a list of pairs where ith elements in the resulting list is a pair where first element in the pair is the ith element in the first list and second element in the pair is the ith element in the second list? I don't understand what *
mean in val pairlists :'a list * 'b list -> ('a * 'b)list
If that was what I was suppose to do then here is what i tried:
let pairlists (l : 'a list) (m : 'b list) =
if l.Length <> m.Length then
failwith "the two lists does not have the same size"
else
[for i in 1 .. l.Length do
yield (l.Head,m.Head)
]
I'm not sure how to do it as I don't know how can i iterate through the lists?
Help
f#
add a comment |
I need to do create a function in F# to pair elements in two lists together(See (ii) in the following picture) :
I'm actually not completely sure what the question is asking: Do I need to create a function that takes two lists and return a list of pairs where ith elements in the resulting list is a pair where first element in the pair is the ith element in the first list and second element in the pair is the ith element in the second list? I don't understand what *
mean in val pairlists :'a list * 'b list -> ('a * 'b)list
If that was what I was suppose to do then here is what i tried:
let pairlists (l : 'a list) (m : 'b list) =
if l.Length <> m.Length then
failwith "the two lists does not have the same size"
else
[for i in 1 .. l.Length do
yield (l.Head,m.Head)
]
I'm not sure how to do it as I don't know how can i iterate through the lists?
Help
f#
add a comment |
I need to do create a function in F# to pair elements in two lists together(See (ii) in the following picture) :
I'm actually not completely sure what the question is asking: Do I need to create a function that takes two lists and return a list of pairs where ith elements in the resulting list is a pair where first element in the pair is the ith element in the first list and second element in the pair is the ith element in the second list? I don't understand what *
mean in val pairlists :'a list * 'b list -> ('a * 'b)list
If that was what I was suppose to do then here is what i tried:
let pairlists (l : 'a list) (m : 'b list) =
if l.Length <> m.Length then
failwith "the two lists does not have the same size"
else
[for i in 1 .. l.Length do
yield (l.Head,m.Head)
]
I'm not sure how to do it as I don't know how can i iterate through the lists?
Help
f#
I need to do create a function in F# to pair elements in two lists together(See (ii) in the following picture) :
I'm actually not completely sure what the question is asking: Do I need to create a function that takes two lists and return a list of pairs where ith elements in the resulting list is a pair where first element in the pair is the ith element in the first list and second element in the pair is the ith element in the second list? I don't understand what *
mean in val pairlists :'a list * 'b list -> ('a * 'b)list
If that was what I was suppose to do then here is what i tried:
let pairlists (l : 'a list) (m : 'b list) =
if l.Length <> m.Length then
failwith "the two lists does not have the same size"
else
[for i in 1 .. l.Length do
yield (l.Head,m.Head)
]
I'm not sure how to do it as I don't know how can i iterate through the lists?
Help
f#
f#
asked Dec 28 '18 at 19:37
nafhgoodnafhgood
1858
1858
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You iterate through the lists by pattern matching, which is done using the match ... with
expression.
For example:
f l =
match l with
| head :: tail -> "This list has at least one element!"
| _ -> "This list is empty"
> f [1]
> "This list has at least one element!"
> f [1; 2]
> "This list has at least one element!"
> f
> "This list is empty"
In your case, there may be several possibilities: both lists are empty, one is empty and the other is not, or both have at least one element:
pairlists l m =
match l, m with
| (lhead :: ltail), (mhead :: mtail) -> // both non-empty
| , (mhead :: mtail) -> // first list empty, second is not
| (lhead :: ltail), -> // second is empty, first is not
| , -> // both are empty
Now all you need to do is decide what the result of the function should be in each one of these four cases, and then write it down.
I am not going to give you the full answer, since this is obviously homework, but here's another hint: pairing two non-empty lists means pairing their heads (i.e. first elements), then pairing their tails, then attaching the two together.
add a comment |
The *
in the function signature means that the input is a tuple of two lists and the output is list of tuples.
Simplest way to achieve this is to use List.zip function:
let pairlists ((a, b) : 'a list * 'b list) =
List.zip a b
Here we define the input parameter to be tuple of 'a list and 'b list and pass the lists to List.zip : 'T1 list -> 'T2 list -> ('T1 * 'T2) list
accordingly
This is homework. I doubtList.zip
is allowed.
– Fyodor Soikin
Dec 28 '18 at 21:13
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when usingzip
, it's best to provide a more robust suggestion.
– ChiefTwoPencils
Dec 29 '18 at 0:40
add a comment |
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
});
}
});
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%2f53963517%2ff-making-list-of-pairs-from-two-lists%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
You iterate through the lists by pattern matching, which is done using the match ... with
expression.
For example:
f l =
match l with
| head :: tail -> "This list has at least one element!"
| _ -> "This list is empty"
> f [1]
> "This list has at least one element!"
> f [1; 2]
> "This list has at least one element!"
> f
> "This list is empty"
In your case, there may be several possibilities: both lists are empty, one is empty and the other is not, or both have at least one element:
pairlists l m =
match l, m with
| (lhead :: ltail), (mhead :: mtail) -> // both non-empty
| , (mhead :: mtail) -> // first list empty, second is not
| (lhead :: ltail), -> // second is empty, first is not
| , -> // both are empty
Now all you need to do is decide what the result of the function should be in each one of these four cases, and then write it down.
I am not going to give you the full answer, since this is obviously homework, but here's another hint: pairing two non-empty lists means pairing their heads (i.e. first elements), then pairing their tails, then attaching the two together.
add a comment |
You iterate through the lists by pattern matching, which is done using the match ... with
expression.
For example:
f l =
match l with
| head :: tail -> "This list has at least one element!"
| _ -> "This list is empty"
> f [1]
> "This list has at least one element!"
> f [1; 2]
> "This list has at least one element!"
> f
> "This list is empty"
In your case, there may be several possibilities: both lists are empty, one is empty and the other is not, or both have at least one element:
pairlists l m =
match l, m with
| (lhead :: ltail), (mhead :: mtail) -> // both non-empty
| , (mhead :: mtail) -> // first list empty, second is not
| (lhead :: ltail), -> // second is empty, first is not
| , -> // both are empty
Now all you need to do is decide what the result of the function should be in each one of these four cases, and then write it down.
I am not going to give you the full answer, since this is obviously homework, but here's another hint: pairing two non-empty lists means pairing their heads (i.e. first elements), then pairing their tails, then attaching the two together.
add a comment |
You iterate through the lists by pattern matching, which is done using the match ... with
expression.
For example:
f l =
match l with
| head :: tail -> "This list has at least one element!"
| _ -> "This list is empty"
> f [1]
> "This list has at least one element!"
> f [1; 2]
> "This list has at least one element!"
> f
> "This list is empty"
In your case, there may be several possibilities: both lists are empty, one is empty and the other is not, or both have at least one element:
pairlists l m =
match l, m with
| (lhead :: ltail), (mhead :: mtail) -> // both non-empty
| , (mhead :: mtail) -> // first list empty, second is not
| (lhead :: ltail), -> // second is empty, first is not
| , -> // both are empty
Now all you need to do is decide what the result of the function should be in each one of these four cases, and then write it down.
I am not going to give you the full answer, since this is obviously homework, but here's another hint: pairing two non-empty lists means pairing their heads (i.e. first elements), then pairing their tails, then attaching the two together.
You iterate through the lists by pattern matching, which is done using the match ... with
expression.
For example:
f l =
match l with
| head :: tail -> "This list has at least one element!"
| _ -> "This list is empty"
> f [1]
> "This list has at least one element!"
> f [1; 2]
> "This list has at least one element!"
> f
> "This list is empty"
In your case, there may be several possibilities: both lists are empty, one is empty and the other is not, or both have at least one element:
pairlists l m =
match l, m with
| (lhead :: ltail), (mhead :: mtail) -> // both non-empty
| , (mhead :: mtail) -> // first list empty, second is not
| (lhead :: ltail), -> // second is empty, first is not
| , -> // both are empty
Now all you need to do is decide what the result of the function should be in each one of these four cases, and then write it down.
I am not going to give you the full answer, since this is obviously homework, but here's another hint: pairing two non-empty lists means pairing their heads (i.e. first elements), then pairing their tails, then attaching the two together.
answered Dec 28 '18 at 19:52
Fyodor SoikinFyodor Soikin
42.3k56497
42.3k56497
add a comment |
add a comment |
The *
in the function signature means that the input is a tuple of two lists and the output is list of tuples.
Simplest way to achieve this is to use List.zip function:
let pairlists ((a, b) : 'a list * 'b list) =
List.zip a b
Here we define the input parameter to be tuple of 'a list and 'b list and pass the lists to List.zip : 'T1 list -> 'T2 list -> ('T1 * 'T2) list
accordingly
This is homework. I doubtList.zip
is allowed.
– Fyodor Soikin
Dec 28 '18 at 21:13
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when usingzip
, it's best to provide a more robust suggestion.
– ChiefTwoPencils
Dec 29 '18 at 0:40
add a comment |
The *
in the function signature means that the input is a tuple of two lists and the output is list of tuples.
Simplest way to achieve this is to use List.zip function:
let pairlists ((a, b) : 'a list * 'b list) =
List.zip a b
Here we define the input parameter to be tuple of 'a list and 'b list and pass the lists to List.zip : 'T1 list -> 'T2 list -> ('T1 * 'T2) list
accordingly
This is homework. I doubtList.zip
is allowed.
– Fyodor Soikin
Dec 28 '18 at 21:13
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when usingzip
, it's best to provide a more robust suggestion.
– ChiefTwoPencils
Dec 29 '18 at 0:40
add a comment |
The *
in the function signature means that the input is a tuple of two lists and the output is list of tuples.
Simplest way to achieve this is to use List.zip function:
let pairlists ((a, b) : 'a list * 'b list) =
List.zip a b
Here we define the input parameter to be tuple of 'a list and 'b list and pass the lists to List.zip : 'T1 list -> 'T2 list -> ('T1 * 'T2) list
accordingly
The *
in the function signature means that the input is a tuple of two lists and the output is list of tuples.
Simplest way to achieve this is to use List.zip function:
let pairlists ((a, b) : 'a list * 'b list) =
List.zip a b
Here we define the input parameter to be tuple of 'a list and 'b list and pass the lists to List.zip : 'T1 list -> 'T2 list -> ('T1 * 'T2) list
accordingly
answered Dec 28 '18 at 20:16
WoteWote
986
986
This is homework. I doubtList.zip
is allowed.
– Fyodor Soikin
Dec 28 '18 at 21:13
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when usingzip
, it's best to provide a more robust suggestion.
– ChiefTwoPencils
Dec 29 '18 at 0:40
add a comment |
This is homework. I doubtList.zip
is allowed.
– Fyodor Soikin
Dec 28 '18 at 21:13
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when usingzip
, it's best to provide a more robust suggestion.
– ChiefTwoPencils
Dec 29 '18 at 0:40
This is homework. I doubt
List.zip
is allowed.– Fyodor Soikin
Dec 28 '18 at 21:13
This is homework. I doubt
List.zip
is allowed.– Fyodor Soikin
Dec 28 '18 at 21:13
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when using
zip
, it's best to provide a more robust suggestion.– ChiefTwoPencils
Dec 29 '18 at 0:40
@FyodorSoikin, although it may be homework, good questions and answers are good. That said, they do specifically state that the lists should be of equal size and since anything else results in an exception when using
zip
, it's best to provide a more robust suggestion.– ChiefTwoPencils
Dec 29 '18 at 0:40
add a comment |
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.
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%2f53963517%2ff-making-list-of-pairs-from-two-lists%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