【发布时间】:2018-11-25 21:30:53
【问题描述】:
我正在寻求帮助以解决 F# 中的编程练习。我必须创建一个列表,其中列出了书籍和电影。所有与电影同名的书籍都应该列在另一个列表中。我将我到目前为止所做的事情与输入内容以及我应该得到的结果联系起来。提前谢谢你。
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)
这里是输入:
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 }
这就是我应该得到的结果:
Set.ofList [
{ bookName = "The Hobbit"; pages = 304N }
{ bookName = "The Fellowship of the Ring"; pages = 700N }
【问题讨论】:
-
我不确定我是否理解问题所在 - 您的示例结果似乎不像将同名的书籍和电影结合在一起。另外,您是想仅使用递归从头开始编写,还是想使用内置库?
-
我链接了一个错误的样本结果,这是正确的。
-
谢谢!为了能够提供帮助,如果您能解释解决问题的策略也是很好的 - 您发布了一些代码示例,但它甚至没有编译,也没有说明您计划如何解决问题。您能否就您如何看待这个问题提供一些文字描述? (而且,无论您是想使用递归还是内置函数。)
-
F# divide lists的可能重复
-
我想使用递归并且没有内置函数。我的第一个计划是将每本书与列表中的每部电影进行比较,并删除所有标题不同的书和电影。所以最后我会有一个列表,里面只有那些也以电影形式存在的书。我不知道这是否是要走的路。谢谢
标签: list merge split f# divide