Haskell: The last statement in the "do" construct must be an expression

Hey, I'm sorry I got an error message here, but I tried everything I could and nothing seems relevant. This code generates an error:

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

I know that this is due to alignment, but I correctly aligned all the calls in the "load" function. What am I doing wrong?

Thanks -A

+3
source share
2 answers

The last line in the doindented expression is too far.

Alternatively, you can simply write

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

, , : load: putStr String, Node. createNode: Pathnode , x y Float, .

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.

+3

All Articles