Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 如何将IO字符串转换为代数类型的数据;产品“;一个txt文件?_Haskell_Io - Fatal编程技术网

Haskell 如何将IO字符串转换为代数类型的数据;产品“;一个txt文件?

Haskell 如何将IO字符串转换为代数类型的数据;产品“;一个txt文件?,haskell,io,Haskell,Io,我必须要求用户在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 Pric

我必须要求用户在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()
之后,我必须使用函数loadProducts加载产品。。。它读取上一个函数的文件,并返回类型IO Products,以便用户能够以类型Products的格式可视化文件中的内容

loadTable:: IO Produtos
loadTable = do
         s<-readFile produtoPath
    --   return generateList (map words)
loadTable::IO产品
loadTable=do
s您可能需要从
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]
adicionaProd=do
PUTSR“产品代码:”

代码尝试使用
show::show a=>a->String
[(代码、名称、价格)]
转换为
String
,并使用
read::read a=>String->a
String
转换为
[(代码、名称、价格)]
 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]