1

Hello i was wondering how can you unwrap a value at a later time in the IO monad? If a<-expression binds the result to a then can't i use (<-expression) as a parameter for a given method eg: method (<-expression) where method method accepts the result of the evaluation?

Code

let inh=openFile "myfile" WriteMode
let outh=openFile "out.txt" WriteMode 
hPutStrLn (<-outh) ((<-inh)>>=getLine)

I have not entered the Monad chapter just basic <- and do blocks but i suppose it has to do with monads. Then if i want to pass the result if the evaluation to hGetLine can't i use something like:

(<-expression)=>>hGetLine
David Young
  • 10,713
  • 2
  • 33
  • 47
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 4
    `<-` is not a function, it is part of the syntax. It is syntactic sugar for calls to `(>>=)`. This is why you can't use it in that way. If you do not yet want to use these `Monad` methods directly, how about `inh <- openFile ...; outh <- openFile ...; r <- hGetLine inh; hPutStrLn outh r` instead of the `let`s? – David Young Jul 24 '18 at 06:42
  • Ok `variable<-expression` cannot be used without the left argument.So i must enter the `monad` chapter.So i can use it like this? `hPutStrLn (=<>=hGetLine`. – Bercovici Adrian Jul 24 '18 at 07:09
  • yes, it is part of the "`do`-notation". start with the do notation. – Will Ness Jul 24 '18 at 07:12
  • 1
    Was proposal [InlineDoBind](https://gist.github.com/evincarofautumn/9cb3fb0197d2cfc1bc6fe88f7827216a) which similar to what you are thinking. – freestyle Jul 24 '18 at 07:48
  • `<-` and `do`, by definition, relate to monads (ignoring the `ApplicativeDo` extension). I gently suggest reading your chapter on monads. – chepner Jul 24 '18 at 11:11
  • @freestyle: Which I actually submitted as [InlineBindings](https://github.com/ghc-proposals/ghc-proposals/pull/64) but my work on it fizzled out; I hope to get to it at some point still. – Jon Purdy Jul 25 '18 at 22:06

1 Answers1

3

You already understand that <- operator kind of unwraps IO value, but it's actually the syntax of do notation and can be expressed like this (actually I'm not sure, which results you're trying to achieve, but the following example just reads the content from one file and puts the content to another file):

import System.IO

main = do
  inh <- openFile "myfile" ReadMode
  outh <- openFile "out.txt" WriteMode
  inContent <- hGetLine inh
  hPutStrLn outh inContent
  hClose outh

According to documentation hGetLine, hPutStrlLn and hClose accept values of Handle type as an argument, but openFile returns IO Handle, so we need to unwrap it using <- operator

But if you want to use >>= function instead, then this is one of the options of doing it:

import System.IO

writeContentOfMyFile :: Handle -> IO ()
writeContentOfMyFile handler =
  openFile "myfile" ReadMode >>= hGetLine >>= hPutStrLn handler

main =
  withFile "out.txt" WriteMode writeContentOfMyFile
Igor Drozdov
  • 14,690
  • 5
  • 37
  • 53
  • But what i do not understand is when do you use `=<<` and when `>>=` – Bercovici Adrian Jul 24 '18 at 08:15
  • 2
    it mostly the same, the only difference is the order of the arguments. For example, you have `line` of type `IO String` and want to print it. You can do it either by `print =<< a` or by `a >>= print` – Igor Drozdov Jul 24 '18 at 08:30