F# list compare












0















I'm asking for help to solve a programming exercice in F#. I have to create a list where books and movies are listed in. All the books that have the same name as the movies sbould be listed in another list. I link what I've done until now and what the inputs are and what results i should get. Thank you in advance.



type Movie =
{ movieName: string
duration: Nat
fileSize: Nat }

type Book =
{ bookName: string
pages: Nat }

type Activity =
| Watch of Movie
| Read of Book

let booksWithMovie(activities: Activity list): Book list =
match activities with
| ->
| [Read book] -> match activities with
| x :: xs -> match x with
| Watch same -> if (same.bookName = same.movieName) then [same] else
booksWithMovie(xs)


Here are the inputs:



Set.ofList (booksWithMovie [
Read { bookName = "The Hobbit"; pages = 304N }
Watch { movieName = "The Fellowship of the Ring"; duration = 228N; fileSize = 50N }
Read { bookName = "The Name of the Wind"; pages = 662N }
Watch { movieName = "The Emoji Movie"; duration = 86N; fileSize = 1024N }
Watch { movieName = "The Hobbit"; duration = 164N; fileSize = 9001N }
Read { bookName = "The Fellowship of the Ring"; pages = 700N }


And that's the result I should get:



Set.ofList [
{ bookName = "The Hobbit"; pages = 304N }
{ bookName = "The Fellowship of the Ring"; pages = 700N }









share|improve this question

























  • I'm not sure I understand what the question is - you sample result does not seem like something that joins the books and movies with the same title. Also, are you trying to write this from scratch using just recursion, or do you want to use built-in libraries?

    – Tomas Petricek
    Nov 25 '18 at 22:01











  • I linked a wrong sample result, that's the right one.

    – VersionF
    Nov 25 '18 at 22:07






  • 2





    Thanks! To be able to help, it would also be good if you could explain what is your strategy for solving the issue - you posted some code sample, but that does not even compile and it does not illustrate how you plan to solve the problem. Could you just give some textual description about how you are thinking about the problem? (And also, whether you want to use recursion or built-in functions.)

    – Tomas Petricek
    Nov 26 '18 at 1:15











  • Possible duplicate of F# divide lists

    – Markus Deibel
    Nov 26 '18 at 10:41











  • I want to use recursion and no built-in functions. My first plan was to compare every book with every movie that are in the list and remove every book and movie which don't have the same title. So at the end I would have a list with only the books that also exists as a film. I don't know if it is the way to go. Thank you

    – VersionF
    Nov 26 '18 at 10:48


















0















I'm asking for help to solve a programming exercice in F#. I have to create a list where books and movies are listed in. All the books that have the same name as the movies sbould be listed in another list. I link what I've done until now and what the inputs are and what results i should get. Thank you in advance.



type Movie =
{ movieName: string
duration: Nat
fileSize: Nat }

type Book =
{ bookName: string
pages: Nat }

type Activity =
| Watch of Movie
| Read of Book

let booksWithMovie(activities: Activity list): Book list =
match activities with
| ->
| [Read book] -> match activities with
| x :: xs -> match x with
| Watch same -> if (same.bookName = same.movieName) then [same] else
booksWithMovie(xs)


Here are the inputs:



Set.ofList (booksWithMovie [
Read { bookName = "The Hobbit"; pages = 304N }
Watch { movieName = "The Fellowship of the Ring"; duration = 228N; fileSize = 50N }
Read { bookName = "The Name of the Wind"; pages = 662N }
Watch { movieName = "The Emoji Movie"; duration = 86N; fileSize = 1024N }
Watch { movieName = "The Hobbit"; duration = 164N; fileSize = 9001N }
Read { bookName = "The Fellowship of the Ring"; pages = 700N }


And that's the result I should get:



Set.ofList [
{ bookName = "The Hobbit"; pages = 304N }
{ bookName = "The Fellowship of the Ring"; pages = 700N }









share|improve this question

























  • I'm not sure I understand what the question is - you sample result does not seem like something that joins the books and movies with the same title. Also, are you trying to write this from scratch using just recursion, or do you want to use built-in libraries?

    – Tomas Petricek
    Nov 25 '18 at 22:01











  • I linked a wrong sample result, that's the right one.

    – VersionF
    Nov 25 '18 at 22:07






  • 2





    Thanks! To be able to help, it would also be good if you could explain what is your strategy for solving the issue - you posted some code sample, but that does not even compile and it does not illustrate how you plan to solve the problem. Could you just give some textual description about how you are thinking about the problem? (And also, whether you want to use recursion or built-in functions.)

    – Tomas Petricek
    Nov 26 '18 at 1:15











  • Possible duplicate of F# divide lists

    – Markus Deibel
    Nov 26 '18 at 10:41











  • I want to use recursion and no built-in functions. My first plan was to compare every book with every movie that are in the list and remove every book and movie which don't have the same title. So at the end I would have a list with only the books that also exists as a film. I don't know if it is the way to go. Thank you

    – VersionF
    Nov 26 '18 at 10:48
















0












0








0








I'm asking for help to solve a programming exercice in F#. I have to create a list where books and movies are listed in. All the books that have the same name as the movies sbould be listed in another list. I link what I've done until now and what the inputs are and what results i should get. Thank you in advance.



type Movie =
{ movieName: string
duration: Nat
fileSize: Nat }

type Book =
{ bookName: string
pages: Nat }

type Activity =
| Watch of Movie
| Read of Book

let booksWithMovie(activities: Activity list): Book list =
match activities with
| ->
| [Read book] -> match activities with
| x :: xs -> match x with
| Watch same -> if (same.bookName = same.movieName) then [same] else
booksWithMovie(xs)


Here are the inputs:



Set.ofList (booksWithMovie [
Read { bookName = "The Hobbit"; pages = 304N }
Watch { movieName = "The Fellowship of the Ring"; duration = 228N; fileSize = 50N }
Read { bookName = "The Name of the Wind"; pages = 662N }
Watch { movieName = "The Emoji Movie"; duration = 86N; fileSize = 1024N }
Watch { movieName = "The Hobbit"; duration = 164N; fileSize = 9001N }
Read { bookName = "The Fellowship of the Ring"; pages = 700N }


And that's the result I should get:



Set.ofList [
{ bookName = "The Hobbit"; pages = 304N }
{ bookName = "The Fellowship of the Ring"; pages = 700N }









share|improve this question
















I'm asking for help to solve a programming exercice in F#. I have to create a list where books and movies are listed in. All the books that have the same name as the movies sbould be listed in another list. I link what I've done until now and what the inputs are and what results i should get. Thank you in advance.



type Movie =
{ movieName: string
duration: Nat
fileSize: Nat }

type Book =
{ bookName: string
pages: Nat }

type Activity =
| Watch of Movie
| Read of Book

let booksWithMovie(activities: Activity list): Book list =
match activities with
| ->
| [Read book] -> match activities with
| x :: xs -> match x with
| Watch same -> if (same.bookName = same.movieName) then [same] else
booksWithMovie(xs)


Here are the inputs:



Set.ofList (booksWithMovie [
Read { bookName = "The Hobbit"; pages = 304N }
Watch { movieName = "The Fellowship of the Ring"; duration = 228N; fileSize = 50N }
Read { bookName = "The Name of the Wind"; pages = 662N }
Watch { movieName = "The Emoji Movie"; duration = 86N; fileSize = 1024N }
Watch { movieName = "The Hobbit"; duration = 164N; fileSize = 9001N }
Read { bookName = "The Fellowship of the Ring"; pages = 700N }


And that's the result I should get:



Set.ofList [
{ bookName = "The Hobbit"; pages = 304N }
{ bookName = "The Fellowship of the Ring"; pages = 700N }






list merge split f# divide






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 23:20







VersionF

















asked Nov 25 '18 at 21:30









VersionFVersionF

72




72













  • I'm not sure I understand what the question is - you sample result does not seem like something that joins the books and movies with the same title. Also, are you trying to write this from scratch using just recursion, or do you want to use built-in libraries?

    – Tomas Petricek
    Nov 25 '18 at 22:01











  • I linked a wrong sample result, that's the right one.

    – VersionF
    Nov 25 '18 at 22:07






  • 2





    Thanks! To be able to help, it would also be good if you could explain what is your strategy for solving the issue - you posted some code sample, but that does not even compile and it does not illustrate how you plan to solve the problem. Could you just give some textual description about how you are thinking about the problem? (And also, whether you want to use recursion or built-in functions.)

    – Tomas Petricek
    Nov 26 '18 at 1:15











  • Possible duplicate of F# divide lists

    – Markus Deibel
    Nov 26 '18 at 10:41











  • I want to use recursion and no built-in functions. My first plan was to compare every book with every movie that are in the list and remove every book and movie which don't have the same title. So at the end I would have a list with only the books that also exists as a film. I don't know if it is the way to go. Thank you

    – VersionF
    Nov 26 '18 at 10:48





















  • I'm not sure I understand what the question is - you sample result does not seem like something that joins the books and movies with the same title. Also, are you trying to write this from scratch using just recursion, or do you want to use built-in libraries?

    – Tomas Petricek
    Nov 25 '18 at 22:01











  • I linked a wrong sample result, that's the right one.

    – VersionF
    Nov 25 '18 at 22:07






  • 2





    Thanks! To be able to help, it would also be good if you could explain what is your strategy for solving the issue - you posted some code sample, but that does not even compile and it does not illustrate how you plan to solve the problem. Could you just give some textual description about how you are thinking about the problem? (And also, whether you want to use recursion or built-in functions.)

    – Tomas Petricek
    Nov 26 '18 at 1:15











  • Possible duplicate of F# divide lists

    – Markus Deibel
    Nov 26 '18 at 10:41











  • I want to use recursion and no built-in functions. My first plan was to compare every book with every movie that are in the list and remove every book and movie which don't have the same title. So at the end I would have a list with only the books that also exists as a film. I don't know if it is the way to go. Thank you

    – VersionF
    Nov 26 '18 at 10:48



















I'm not sure I understand what the question is - you sample result does not seem like something that joins the books and movies with the same title. Also, are you trying to write this from scratch using just recursion, or do you want to use built-in libraries?

– Tomas Petricek
Nov 25 '18 at 22:01





I'm not sure I understand what the question is - you sample result does not seem like something that joins the books and movies with the same title. Also, are you trying to write this from scratch using just recursion, or do you want to use built-in libraries?

– Tomas Petricek
Nov 25 '18 at 22:01













I linked a wrong sample result, that's the right one.

– VersionF
Nov 25 '18 at 22:07





I linked a wrong sample result, that's the right one.

– VersionF
Nov 25 '18 at 22:07




2




2





Thanks! To be able to help, it would also be good if you could explain what is your strategy for solving the issue - you posted some code sample, but that does not even compile and it does not illustrate how you plan to solve the problem. Could you just give some textual description about how you are thinking about the problem? (And also, whether you want to use recursion or built-in functions.)

– Tomas Petricek
Nov 26 '18 at 1:15





Thanks! To be able to help, it would also be good if you could explain what is your strategy for solving the issue - you posted some code sample, but that does not even compile and it does not illustrate how you plan to solve the problem. Could you just give some textual description about how you are thinking about the problem? (And also, whether you want to use recursion or built-in functions.)

– Tomas Petricek
Nov 26 '18 at 1:15













Possible duplicate of F# divide lists

– Markus Deibel
Nov 26 '18 at 10:41





Possible duplicate of F# divide lists

– Markus Deibel
Nov 26 '18 at 10:41













I want to use recursion and no built-in functions. My first plan was to compare every book with every movie that are in the list and remove every book and movie which don't have the same title. So at the end I would have a list with only the books that also exists as a film. I don't know if it is the way to go. Thank you

– VersionF
Nov 26 '18 at 10:48







I want to use recursion and no built-in functions. My first plan was to compare every book with every movie that are in the list and remove every book and movie which don't have the same title. So at the end I would have a list with only the books that also exists as a film. I don't know if it is the way to go. Thank you

– VersionF
Nov 26 '18 at 10:48














1 Answer
1






active

oldest

votes


















1














Since this looks like a learning exercise, rather than an actual problem (correct me if I'm wrong), I will try to give you a hint so that you can find the solution yourself, rather than just giving the solution.



As you mentioned in the comments, you wanted to iterate over all movies for every book (to check if a movie with the same title exists). This is a good plan. The best way to implement it is to use two recursive functions - one to walk over books and another to walk over movies (looking for a movie with a specific title).



The structure of the code should look something like this:



let rec movieWithTitleExists title (activities:Activity list) = 
match activities with
| -> false
| Watch movie :: xs when movie.movieName = title -> (...)
| x :: xs -> (...)

let rec booksWithMovie (activities: Activity list): Book list =
match activities with
| ->
| Book book :: xs when movieWithTitleExists book.bookName -> (...)
| x :: xs -> (...)


I left a couple of things out, so that you can still learn something from completing the exercise. However, I hope the example of the syntax helps! In movieWithTitleExists, we are looking for movie such that it has the specified title. In booksWithMovie, we are looking for book such that the title is also a movie name.



Filling the (...) in movieWithTitleExists should be easier - you want to return a boolean value, so you either need to return a constant or make a recursive call.



In booksWithMovie, you want to return a list of books, so you will need to call the function recursively and then either just return that, or append the current book to the front using the :: operator.






share|improve this answer
























  • I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

    – VersionF
    Nov 26 '18 at 15:53











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%2f53472204%2ff-list-compare%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









1














Since this looks like a learning exercise, rather than an actual problem (correct me if I'm wrong), I will try to give you a hint so that you can find the solution yourself, rather than just giving the solution.



As you mentioned in the comments, you wanted to iterate over all movies for every book (to check if a movie with the same title exists). This is a good plan. The best way to implement it is to use two recursive functions - one to walk over books and another to walk over movies (looking for a movie with a specific title).



The structure of the code should look something like this:



let rec movieWithTitleExists title (activities:Activity list) = 
match activities with
| -> false
| Watch movie :: xs when movie.movieName = title -> (...)
| x :: xs -> (...)

let rec booksWithMovie (activities: Activity list): Book list =
match activities with
| ->
| Book book :: xs when movieWithTitleExists book.bookName -> (...)
| x :: xs -> (...)


I left a couple of things out, so that you can still learn something from completing the exercise. However, I hope the example of the syntax helps! In movieWithTitleExists, we are looking for movie such that it has the specified title. In booksWithMovie, we are looking for book such that the title is also a movie name.



Filling the (...) in movieWithTitleExists should be easier - you want to return a boolean value, so you either need to return a constant or make a recursive call.



In booksWithMovie, you want to return a list of books, so you will need to call the function recursively and then either just return that, or append the current book to the front using the :: operator.






share|improve this answer
























  • I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

    – VersionF
    Nov 26 '18 at 15:53
















1














Since this looks like a learning exercise, rather than an actual problem (correct me if I'm wrong), I will try to give you a hint so that you can find the solution yourself, rather than just giving the solution.



As you mentioned in the comments, you wanted to iterate over all movies for every book (to check if a movie with the same title exists). This is a good plan. The best way to implement it is to use two recursive functions - one to walk over books and another to walk over movies (looking for a movie with a specific title).



The structure of the code should look something like this:



let rec movieWithTitleExists title (activities:Activity list) = 
match activities with
| -> false
| Watch movie :: xs when movie.movieName = title -> (...)
| x :: xs -> (...)

let rec booksWithMovie (activities: Activity list): Book list =
match activities with
| ->
| Book book :: xs when movieWithTitleExists book.bookName -> (...)
| x :: xs -> (...)


I left a couple of things out, so that you can still learn something from completing the exercise. However, I hope the example of the syntax helps! In movieWithTitleExists, we are looking for movie such that it has the specified title. In booksWithMovie, we are looking for book such that the title is also a movie name.



Filling the (...) in movieWithTitleExists should be easier - you want to return a boolean value, so you either need to return a constant or make a recursive call.



In booksWithMovie, you want to return a list of books, so you will need to call the function recursively and then either just return that, or append the current book to the front using the :: operator.






share|improve this answer
























  • I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

    – VersionF
    Nov 26 '18 at 15:53














1












1








1







Since this looks like a learning exercise, rather than an actual problem (correct me if I'm wrong), I will try to give you a hint so that you can find the solution yourself, rather than just giving the solution.



As you mentioned in the comments, you wanted to iterate over all movies for every book (to check if a movie with the same title exists). This is a good plan. The best way to implement it is to use two recursive functions - one to walk over books and another to walk over movies (looking for a movie with a specific title).



The structure of the code should look something like this:



let rec movieWithTitleExists title (activities:Activity list) = 
match activities with
| -> false
| Watch movie :: xs when movie.movieName = title -> (...)
| x :: xs -> (...)

let rec booksWithMovie (activities: Activity list): Book list =
match activities with
| ->
| Book book :: xs when movieWithTitleExists book.bookName -> (...)
| x :: xs -> (...)


I left a couple of things out, so that you can still learn something from completing the exercise. However, I hope the example of the syntax helps! In movieWithTitleExists, we are looking for movie such that it has the specified title. In booksWithMovie, we are looking for book such that the title is also a movie name.



Filling the (...) in movieWithTitleExists should be easier - you want to return a boolean value, so you either need to return a constant or make a recursive call.



In booksWithMovie, you want to return a list of books, so you will need to call the function recursively and then either just return that, or append the current book to the front using the :: operator.






share|improve this answer













Since this looks like a learning exercise, rather than an actual problem (correct me if I'm wrong), I will try to give you a hint so that you can find the solution yourself, rather than just giving the solution.



As you mentioned in the comments, you wanted to iterate over all movies for every book (to check if a movie with the same title exists). This is a good plan. The best way to implement it is to use two recursive functions - one to walk over books and another to walk over movies (looking for a movie with a specific title).



The structure of the code should look something like this:



let rec movieWithTitleExists title (activities:Activity list) = 
match activities with
| -> false
| Watch movie :: xs when movie.movieName = title -> (...)
| x :: xs -> (...)

let rec booksWithMovie (activities: Activity list): Book list =
match activities with
| ->
| Book book :: xs when movieWithTitleExists book.bookName -> (...)
| x :: xs -> (...)


I left a couple of things out, so that you can still learn something from completing the exercise. However, I hope the example of the syntax helps! In movieWithTitleExists, we are looking for movie such that it has the specified title. In booksWithMovie, we are looking for book such that the title is also a movie name.



Filling the (...) in movieWithTitleExists should be easier - you want to return a boolean value, so you either need to return a constant or make a recursive call.



In booksWithMovie, you want to return a list of books, so you will need to call the function recursively and then either just return that, or append the current book to the front using the :: operator.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 11:34









Tomas PetricekTomas Petricek

199k13290464




199k13290464













  • I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

    – VersionF
    Nov 26 '18 at 15:53



















  • I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

    – VersionF
    Nov 26 '18 at 15:53

















I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

– VersionF
Nov 26 '18 at 15:53





I will try to complet your sample. Just by the first look, I understand the exercice better. I think I already know how to solve it. Thank you !

– VersionF
Nov 26 '18 at 15:53


















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%2f53472204%2ff-list-compare%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

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

Calculate evaluation metrics using cross_val_predict sklearn

Insert data from modal to MySQL (multiple modal on website)