我是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