在Haskell中解析输入错误if-then-else条件

时间:2013-05-30 22:03:30

标签: haskell conditional parse-error

当我尝试编译时,以下do块会抛出错误“输入`conn'上的解析错误”。我尝试过if-then-else语句的许多不同配置无济于事。在添加条件之前数据库逻辑工作,所以没有问题。我在其他地方有太多行吗?有没有办法解决这个问题而不完全修改逻辑?

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 个答案:

答案 0 :(得分:5)

您需要在do之后引入else块,如此:

  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