“parse error on input” in Haskell if-then-else conditional

不羁的心 提交于 2019-12-13 15:24:34

问题


The following do block throws the error "parse error on input `conn'" when I attempt to compile it. I have tried many different configurations of the if-then-else statement to no avail. The database logic worked before I added the conditional, so there isn't a problem with that. Do I have too many lines in the else? Is there any way to fix this without completely revamping the logic?

main = do
   contents <- BL.getContents
   let myData = decode contents :: Maybe Data
   if maybe True (\x -> result x /= "success") myData
      then error ("JSON download failed")
      else let myTrades = process myData
         conn <- connectSqlite3 "trades.db"
         insert <- DB.prepare conn "INSERT INTO trades VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
         DB.executeMany insert $ map (\xs -> map DB.toSql xs) myTrades
         DB.commit conn
         DB.disconnect conn

回答1:


You need to introduce a do block after the else like so:

  else do let myTrades = process myData
          conn <- connectSqlite3 "trades.db"
          insert <- DB.prepare conn "INSERT INTO trades VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
          DB.executeMany insert $ map (\xs -> map DB.toSql xs) myTrades
          DB.commit conn
          DB.disconnect conn


来源:https://stackoverflow.com/questions/16846584/parse-error-on-input-in-haskell-if-then-else-conditional

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!