make函数返回数据结构的标题
有多种数据类型。
data Book =BName| Author deriving (Eq,Show)
data Video = VName deriving (Eq,Show)
data CD =Cname | Auth | NOC deriving (Eq,Show)
data Product = Product Book Video CD deriving (Eq,Show)
make 函数 getTitle 返回结构的名称(BName、CName 或 VName)。 例如 getTitle(Book "name "noname") -> "name" getTitle(Vide“名字”)-> “姓名” 等等。
这可能吗?
现有数据类型 带有字段标题和作者的书籍、带有字段作者的录像带、带有字段标题、作品数量和作者的 CD 盘。 1)创建可以引入该数据类型的数据类型Product 2)make函数getTitle返回标题。 3)创建函数 getTitles 返回产品列表中的所有标题(使用函数 getTtitle) 4)制作函数 bookAuthors 返回产品列表中书籍的作者 5)制作函数lookupTitle::String->[Product]->Maybe Product,返回输入名称的产品 6)制作函数lookupTitles::[String]->[Product]->[Product],其输入参数是名称列表和产品列表,对于每个名称,它从产品列表中获取产品。忽略第一个列表中的名称,而第二个列表中没有产品。使用函数lookipTitle。
这就是全部任务。
data Book = Book String String deriving(Eq,Show)
data Video = Video String deriving(Eq,Show)
data CDisk = CDisk String String Int deriving(Eq,Show)
--titles
class Titleable a where
getTitle :: a ->String
instance Titleable Book where
getTitle (Book title _) = title
instance Titleable Video where
getTitle (Video title) = title
instance Titleable CDisk where
getTitle(CDisk title _ _) = title
--get titles
getTitles (x:xs) = [ getTitle x | x<-xs ]
lookupTitle _ [] =Nothing
lookupTitle::String->[Product]->Maybe Product
lookupTitle a (x:xs) | getTitle x==a =Just x
| otherwise = lookupTitle a (x:xs)
lookupTitles::[String]->[Product]->[Product]
lookupTitles (x:xs) (y:ys) = [y|x<-xs,y<-ys,x==lookupTitle y]
但 1)我不知道如何制作函数bookAuthors(函数如何找到参数是book类型的?) 2)如何制作产品类型?我的意思是它是书籍或视频或CDisk 3)lookupTitle和lookupTitle是否正确?
There are several data types.
data Book =BName| Author deriving (Eq,Show)
data Video = VName deriving (Eq,Show)
data CD =Cname | Auth | NOC deriving (Eq,Show)
data Product = Product Book Video CD deriving (Eq,Show)
make function getTitle which return the name(BName,CName or VName) of structure.
For example
getTitle (Book "name "noname") -> "name"
getTitle (Vide "name") -> "name"
and etc.
Is it possible?
Exist data type Book with fields title and author,Videocassete with field author, CDdisk with fields title,number of compositions and author.
1)create data type Product which can introduce this data types
2)make function getTitle which returns titles.
3)make function getTitles which returns all titles in the list of products(use function getTtitle)
4)make function bookAuthors which return books' authors in the list of products
5)make function lookupTitle::String->[Product]->Maybe Product which returns product with input name
6)make function lookupTitles::[String]->[Product]->[Product] which input params are the list of names and the list of products and for every name it gets products from list of products. Ignor names in the first list without products in the second list for them.Use function lookipTitle.
That's all task.
data Book = Book String String deriving(Eq,Show)
data Video = Video String deriving(Eq,Show)
data CDisk = CDisk String String Int deriving(Eq,Show)
--titles
class Titleable a where
getTitle :: a ->String
instance Titleable Book where
getTitle (Book title _) = title
instance Titleable Video where
getTitle (Video title) = title
instance Titleable CDisk where
getTitle(CDisk title _ _) = title
--get titles
getTitles (x:xs) = [ getTitle x | x<-xs ]
lookupTitle _ [] =Nothing
lookupTitle::String->[Product]->Maybe Product
lookupTitle a (x:xs) | getTitle x==a =Just x
| otherwise = lookupTitle a (x:xs)
lookupTitles::[String]->[Product]->[Product]
lookupTitles (x:xs) (y:ys) = [y|x<-xs,y<-ys,x==lookupTitle y]
but
1)I don't know how to make function bookAuthors(how can function find that the parameter is book-type?)
2)how to make Product type?I mean it is either Book or Video or CDisk
3)lookupTitle and lookupTitle are correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您确定需要这样的数据类型吗?
意味着一本书要么是 BName(书名?),要么是作者?
我怀疑你想要类似的东西,
例如一本书有书名和作者
如果你想捕捉“可标题”事物的本质,你可以使用类型类。
并为其他类型编写实例。
Are you sure you want the data types like this?
Means a Book is either a BName (book name?) or an Author?
I suspect you want something similar to
e.g. a book has a book name and an author
If you want to capture the essence of "titleable" things you could use type classes.
And write instances for your other types.
从任务描述来看,听起来他们正在寻找的数据类型是这样的:
然后可以使用模式匹配来实现函数
getTitle
。我将把剩下的功能的实现留给你。
From the task description, it sounds like the data type they are looking for is something like this:
The function
getTitle
can then be implemented using pattern matching.I'll leave the implementation of the remaining functions to you.