简体   繁体   English

F#列表比较

[英]F# list compare

I'm asking for help to solve a programming exercice in F#. 我正在寻求帮助来解决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 }

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. movieWithTitleExists ,我们正在寻找具有指定标题的电影。 In booksWithMovie , we are looking for book such that the title is also a movie name. booksWithMovie ,我们正在寻找书,使得标题也是电影名称。

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. movieWithTitleExists填充(...)应该更容易-您想返回一个布尔值,因此您需要返回一个常量或进行递归调用。

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. booksWithMovie ,您想返回一本书籍列表,因此您将需要递归调用该函数,然后要么返回它,要么使用::运算符将当前书籍追加到最前面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM