jeudi 16 novembre 2017

How to update variable in IO/access variable made in if statement in haskell

I have some code where I create an object at the beginning. Then I want to run various updates on the object depending on different commands.

main = do
    putStrLn "Enter command, c x y, d x y"
    s <- getLine
    let object = newObject
    if head s == 'c' then
        let object2 = updateObject object s in
        print object
    else if head s == 'd' then
        let object2 = updateObject object s in
        print object
    else do
        print "Error, try again"
        main
    putStrLn "Enter new command"
    c <- getLine
    print $ updateObject object2 c

Error:

Variable not in scope: object2 :: Object

How do I make the object "global" for the main function so that changes done to the object inside the if statements can be used later?

How can I do this in a better way?

Aucun commentaire:

Enregistrer un commentaire