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?
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.