有几种数据类型.
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)
Run Code Online (Sandbox Code Playgroud)
make function getTitle返回结构名称(BName,CName或VName).例如getTitle(Book"name"noname") - >"name"getTitle(Vide"name") - >"name"等.
可能吗?
存在数据类型Book with field title and author,Videocassete with field author,CDdisk with fields title,composition of composition and author.1)创建数据类型可以引入此数据类型的产品2)使函数getTitle返回标题.3)使函数getTitles返回产品列表中的所有标题(使用函数getTtitle)4)使函数bookAuthors返回产品列表中的书籍作者5)使函数lookupTitle :: String - > [Product] - > Maybe返回带有输入名称的产品的产品6)使函数lookupTitles :: [String] - > [Product] - > [Product]输入params是名称列表和产品列表,并且每个名称从列表中获取产品产品.忽略第一个列表中的名称,而不是第二个列表中的产品.使用函数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]
Run Code Online (Sandbox Code Playgroud)
但是1)我不知道如何制作函数bookAuthors(函数怎么能找到参数是book-type?)2)如何制作产品类型?我的意思是它是Book或Video或CDisk 3)lookupTitle和lookupTitle是正确的?
Jef*_*ter 11
您确定要的是这样的数据类型吗?
data Book = BName | Author deriving (Eq,Show)
Run Code Online (Sandbox Code Playgroud)
是指书籍是BName(书名?)还是作者?
我怀疑你想要类似的东西
data Book = Book BName Author
Run Code Online (Sandbox Code Playgroud)
例如,一本书有书名和作者
如果你想捕捉"可标题"事物的本质,你可以使用类型类.
class Titleable a where
getTitle :: a -> String
instance Titleable Book where
getTitle (Book title _) = title
Run Code Online (Sandbox Code Playgroud)
并为您的其他类型编写实例.