我必须要求用户在Products.txt中输入产品的代码、名称和价格。
produtoPath::FilePath
produtoPath = "Products.txt"
adicionaProd::IO()
adicionaProd = do
putStr "Product's Code:"
cod<-getLine
putStr "Product's Name:"
nom<-getLine
putStr "Product's Price:"
pre<-getLine
-- appendFile produtoPath
putStr "Do you want to add some other product?"
resp <- getLine
if ((resp == "y")) then adicionaProd else return()因此,在添加产品之后,我必须创建另一个函数来读取Products.txt的内容,并返回产品...类型Products = (Code,Name,Price),我不知道该怎么做...我试过了,但我做不到。
generateList:: [[String]]-> Produtos
generateList [] = []
--generateList [[cod,nom,pre]:[]] = [(read:cod)::]在那之后,我必须用loadProducts函数加载产品...它读取前一个函数的文件,并返回IO Products类型,以便用户能够以Products类型的格式可视化文件中的内容。
loadTable:: IO Produtos
loadTable = do
s<-readFile produtoPath
-- return generateList (map words)综上所述:我不知道我应该如何将数据保存在"addProd“函数中,去读取文件并生成产品cpm的generateList函数,并将它们加载到用户用的loadTable function.Can中,你有什么帮助吗?我做了什么的完整代码:https://ideone.com/Al9ZLf
发布于 2017-03-29 15:33:02
您可能需要从adicionaProd返回输入数据。因此,它的类型必须变成
type Products = [(Code, Name, Price)]
adicionaProd :: IO Products然后,调整代码本身
adicionaProd = do
putStr "Product's Code:"
cod<-getLine
putStr "Product's Name:"
nom<-getLine
putStr "Product's Price:"
pre<-getLine
let prod :: (Code, Name, Price)
prod = (read cod, nom, read pre)
putStr "Do you want to add some other product?"
resp <- getLine
if resp == "y" then do
rest <- adicionaProd
return (prod : rest)
else
return [prod]其思想是:如果响应是"yes",那么递归地请求产品列表,并将其绑定到rest。然后,您返回一个包含您的产品的列表,然后返回其他产品。如果响应为"no",则返回一个仅包含第一个产品的列表。
https://stackoverflow.com/questions/43081598
复制相似问题