开发者

Better way to perform simple I/O in main

开发者 https://www.devze.com 2023-01-14 14:23 出处:网络
Quick question I\'ve encountered while getting my feet under me in Haskell related to this quick test:

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 :)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消