Haskell: How to read in command line args as int?

haskell, type-conversion

Solution

The problem is with precendence: Type signatures always try to apply to the whole expression (only scoped using parenthesis). So your `disp $ read $ head args :: Int` parses as `(disp $ read $ head args) :: Int`, which is obviously not correct. You can either use parenthesis like so:

disp (read $ head args :: Int)

or omit the type signature, as GHC can infer it in this case:

disp $ read $ head args

This code still won't work as-is, because you're in the IO monad so you need to produce IO actions. You can do this by printing the result, for example:

putStrLn $ disp $ read $ head args

Problem

I am trying to get an int value from the command line and pass it to the `disp` function. ``` import System(getArgs) main = do args <- getArgs disp $ read $ head args :: Int disp n = take n $ repeat 'S' ``` The error given by ghc is ``` Couldn't match expected type `Int' with actual type `[Char]' In the expression: disp $ read $ head args :: Int In the expression: do { args <- getArgs; disp $ read $ head args :: Int } In an equation for `main': main = do { args <- getArgs; disp $ read $ head args :: Int } ``` Thanks.

Original source