Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IO as a side-effect

Tags:

io

haskell

Am I able to write a function executing IO as a side-effect of it? For example:

f :: Int -> Int
f n = putStr "text" >> return n*2

Obviously I don't have any way to write that code without it being completely incorrect, but that should at least show roughly what I'm looking to do.

like image 489
Allan Avatar asked Jul 21 '26 02:07

Allan


1 Answers

Your function is almost right. If it has a side effect then it needs type IO. Also, function application binds tighter than infix. Fixing these results in:

f :: Int -> IO Int
f n = putStr "text" >> return (n*2)
like image 160
Thomas M. DuBuisson Avatar answered Jul 23 '26 17:07

Thomas M. DuBuisson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!