【问题标题】:"parse error on input" in Haskell if-then-else conditionalHaskell if-then-else 条件中的“输入解析错误”
【发布时间】:2013-05-26 15:26:26
【问题描述】:

以下 do 块在我尝试编译时抛出错误“输入 `conn' 上的解析错误”。我尝试了许多不同的 if-then-else 语句配置,但均无济于事。数据库逻辑在我添加条件之前工作,所以没有问题。我在 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

【问题讨论】:

  • 这有两个问题。第一个是下面答案中描述的那个;我需要在“其他”之后“做”。此外,最后四行需要缩进以与“let”中的“l”在同一列中。

标签: haskell conditional parse-error


【解决方案1】:

您需要在else 之后引入一个do 块,如下所示:

  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

【讨论】:

  • 我还需要将最后四列与“let”中的“l”对齐。不过,这是主要问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
  • 2013-11-21
  • 2021-02-09
  • 2021-11-13
  • 1970-01-01
相关资源
最近更新 更多