2011-05-04 53 views
3

嗨,抱歉在這裏轉儲錯誤消息,但我嘗試了所有可以找到的東西,而且沒有任何相關性。此代碼正在生成錯誤:Haskell:'do'構造中的最後一個語句必須是一個表達式

import System.Environment 
import System.Directory 
import System.IO 
import Data.List 

data Node = PathNode String Float Float [String] | NoNode deriving (Show) 


main = do 
    (filename:args) <- getArgs 
    load filename 


load :: String -> IO() 
load fileName = do 
    contents <- readFile fileName 
    let pathStrings = lines contents 
     first = head pathStrings 
     args = lines first 
     path = createNode args 
     putStr path 


createNode [String] -> Node 
createNode (name:x:y:paths) = PathNode name x y paths 
createNode [] = NoNode 

我知道它與對齊方式有關,但我已正確調整了「加載」函數中的所有調用。我究竟做錯了什麼?

感謝 -A

+0

參見http://stackoverflow.com/questions/4513396/problem-with-do-construct-in-haskell – 2011-05-04 16:50:17

回答

6

do表達的最後一行縮進的太多。

另外,你可以只寫

load :: String -> IO() 
load fileName = 
    putStr . createNode . lines . head . lines =<< readFile filename 
3

只有錯誤,我可以找到除了identation是:在負載 :putStr預計字符串,而路徑是類型節點。在createNode中的 :Pathnode需要x和y的類型爲Float,而您需要Strings。

load :: String -> IO() 
load fileName = do 
    contents <- readFile fileName 
    let pathStrings = lines contents 
     first = head pathStrings 
     args = lines first 
     path = createNode args 
    putStr $ show path 


createNode :: [String] -> Node 
createNode (name:x:y:paths) = PathNode name (read x) (read y) paths 
createNode [] = NoNode 

這解決了使用show和read描述的兩種類型的錯誤。

相關問題