make function which returns the title of data structure [closed]
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 an开发者_JAVA百科d 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?
Are you sure you want the data types like this?
data Book = BName | Author deriving (Eq,Show)
Means a Book is either a BName (book name?) or an Author?
I suspect you want something similar to
data Book = Book BName Author
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.
class Titleable a where
getTitle :: a -> String
instance Titleable Book where
getTitle (Book title _) = title
And write instances for your other types.
From the task description, it sounds like the data type they are looking for is something like this:
type Title = String
type Author = String
data Product = Book Title Author
| Video Title
| CD Title Integer Author
deriving (Eq, Show)
The function getTitle
can then be implemented using pattern matching.
getTitle (Book title _) = title
getTitle (Video title) = title
getTitle (CD title _ _) = title
I'll leave the implementation of the remaining functions to you.
精彩评论