2011-05-04 35 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描述的两种类型的错误。

相关问题