I'm relatively new to Haskell and can't figure out what I'm doing wrong in this block.
The point of the block is to take a list and ask for an Int, which is then used as the index of the list element. If the value input isn't an Int, it results in an error and loops the block again.
returnIndex' :: [String] -> IO ()
returnIndex' xs = do
putStrLn "Index Number:"
n <- getLine
att <- try(return(read n :: Int)) :: IO(Either SomeException Int)
case att of
Left _ ->
putStrLn "Has To Be Int" >> returnIndex' xs
Right index ->
print (xs!!index)
It compiles just fine, but when I try to run it, I get:
- No instance for (Show ([String] -> IO ())) arising from a use of `print' (maybe you haven't applied a function to enough arguments?)
- In a stmt of an interactive GHCi command: print it
The only print in the code is the last line, which looks pretty harmless to me. Isn't print essentially just putStrLn.show? Is the error that it takes [String] instead of just String?
Edit: Thank you for the comments but the problem inexplicably resolved itself, all I did was close and reopen the command prompt a few minutes later. I don't know the common stack overflow etiquette now, should I just delete my question?
returnIndex'
, instead ofreturnIndex' ["List", "Of", "Values"]
? – Willem Van OnsemsomeExpression
, then implicitly, you writeprint someExpression
, so theprint
is not from the function, but from the one in theghci
prompt. – Willem Van Onsemsucc
in GHCi you get a similar error. You should pass its arguments as insucc 4
so that the final result can be shown. In your case, enter something likereturnIndex' ["aa", "bb"]
– chi