我是java程序员现在正在阅读和学习haskell。我正在尝试编写一个简单的程序,使用HDBC postgres驱动程序连接(并断开连接)到postgres数据库。为了简单起见,我没有任何其他逻辑。了解Haskell postgresql连接函数类型错误
它正在抛出一个函数类型错误。我缩进代码正确,如果我删除断开连接,然后它与定义的类型。
有人能说明我缺少什么定义这个函数的类型吗?我会提供你的帮助。
谢谢!
示例代码:
import Database.HDBC
import Database.HDBC.PostgreSQL
import Database.HaskellDB
import Database.HaskellDB.HDBC.PostgreSQL
tryConnect :: Int -> (Database -> IO Connection) ->()
tryConnect id =
do
c <- postgresqlConnect [("host","dbhost"),("dbname","db1"),("user","user1"),("password","test")]
disconnect c
return()
我从GHCI
Couldn't match expected type `(Database -> IO Connection) -> a'
against inferred type `IO()'
In a stmt of a 'do' expression: disconnect c
In the expression:
do { c <- postgresqlConnect
[("host", "dbhost"), ("dbname", "db1"), ....];
disconnect c;
return() }
In the definition of `insrt':
insrt id
= do { c <- postgresqlConnect [("host", "dbhost"), ....];
disconnect c;
return() }
失败,加载的模块得到以下错误:无。
谢谢Jeffrey。类型是由我定义的,而不是由函数定义的。我之前并不清楚。最初我尝试过这种类型, – stevron 2011-04-17 02:51:11
tryConnect :: Int - > IO()。这引发了以下异常 – stevron 2011-04-17 02:52:04
我的坏..评论被打破。 {无法匹配预期类型'IO()' 针对推断类型'(数据库 - > ma) - > b' 在'do'表达式中: c < - postgresqlConnect [(“host” “dbhost”),(“dbname”,“db1”),(“user”,“user1”) (“password”,“test”)] 在{ (“主机”,“dbhost”),(“dbname”,“db1”),....]。 断开c; return()} } – stevron 2011-04-17 02:52:42