JohnCohorn
JohnCohorn

Reputation: 23

Better way to perform simple I/O in main

Quick question I've encountered while getting my feet under me in Haskell related to this quick test:

module Main where
main :: IO()
main = putStrLn (show (inc 3))

inc :: (Num a) => a -> a
inc x = x+1

Is there a better way to output the value of the inc function? I could not get output without using nested parens to force evaluation order. With fewer parens I receive type errors. Just figure there must be a better way.

Thanks if you can clear my head :)

Upvotes: 2

Views: 211

Answers (1)

sepp2k
sepp2k

Reputation: 370082

First of all: parentheses don't force evaluation order.

To get rid of parentheses you can use $ which has very low precedence and thus allows you to get rid of parentheses for the last argument.

For this particular case there's also the print function which is defined as putStrLn . show, so you can do print (inc 3) or print $ inc 3.

Upvotes: 12

Related Questions