jeudi 16 novembre 2017

How to use let inside if block inside a do notation in haskell

I'm getting an error with the following code:

main = do
    putStrLn "Enter parameter"
    parameter <- getLine
    if head parameter == "c" then
        let object = getObject parameter
        print object
    else
        putStrLn "Error, try again"
        main

The error I get is:

parse error on input `print'

when trying to print the object. If I instead try to print the value of the function without saving it with let it works just fine, but I need to save the object for later use.

How should the syntax be to make it work?

Also in the "else" part I get the following error:

The function `putStrLn' is applied to two arguments,
    but its type `String -> IO ()' has only one
    In the expression: putStrLn "Error" main

It thinks I try to run main as a parameter to putStrLn, but what I really want to do is first show the error and then run main again. How can I fix this?

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire