“输入解析错误”在Haskell if-then-else有条件的情况下 [英] "parse error on input" in Haskell if-then-else conditional

查看:140
本文介绍了“输入解析错误”在Haskell if-then-else有条件的情况下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我试图编译它时,下面的块会抛出错误输入`conn'解析错误。我已经尝试了许多不同的if-then-else语句的配置,无济于事。数据库逻辑在我添加条件之前就工作了,所以这没有问题。我在其他线路上有太多线路吗?有没有什么办法可以解决这个问题,而不需要完全修改逻辑?

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


推荐答案

您需要在后面引入 do else 就像这样:

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

这篇关于“输入解析错误”在Haskell if-then-else有条件的情况下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆