Questions tagged [io-monad]

Monadic interface to IO in pure functional languages, especially Haskell.

Monadic interface to IO in pure functional languages, especially Haskell.

194 questions
23
votes
1 answer

What is the difference between "evaluate" and "return $!"?

Here's an extract from the documentation of evaluate: Control.Exception.Base.evaluate :: a -> IO a evaluate x is not the same as return $! x A correct definition is evaluate x = (return $! x) >>= return (source) These seem to have the same…
HNoob
  • 591
  • 4
  • 13
21
votes
2 answers

How to get normal value from IO action in Haskell

I have the following function: get :: Chars -> IO Chars get cs = do char <- getChar let (dats, idx) = (curData cs, curIndex cs) let (x,y:xs) = splitAt idx dats let replacement = x ++ (ord char) : xs return $ Chars replacement…
Benjamin Kovach
  • 3,190
  • 1
  • 24
  • 38
19
votes
4 answers

JavaScript function composition by chaining

I have checked the possibility of duplicate question, and cannot find the exact solution. I wrote some function chain code in JavaScript as below, and works fine. var log = function(args) { console.log(args) return function(f) { return…
user1028880
18
votes
2 answers

Haskell -- dual personality IO / ST monad?

I have some code that currently uses a ST monad for evaluation. I like not putting IO everywhere because the runST method produces a pure result, and indicates that such result is safe to call (versus unsafePerformIO). However, as some of my code…
gatoatigrado
  • 16,580
  • 18
  • 81
  • 143
17
votes
2 answers

Why can't Haskell be tricked into performing IO operations by using strict evaluation?

I'm just learning Haskell and IO monads. I'm wondering why wouldn't this force the program to output "hi" as well as "bye": second a b = b main = print ((second $! ((print "hi") >>= (\r -> return ()))) "bye") As far as I understand, the $! operator…
Juan
  • 15,274
  • 23
  • 105
  • 187
16
votes
2 answers

What is the best way to manage resources in a monad stack like ExceptT a IO?

For better or for worse, Haskell's popular Servant library has made it common-place to run code in a monad transformer stack involving ExceptT err IO. Servant's own handler monad is ExceptT ServantErr IO. As many argue, this is a somewhat…
Elliot Cameron
  • 5,235
  • 2
  • 27
  • 34
12
votes
2 answers

cats-effect:How to transform Map[x,IO[y]] to IO[Map[x,y]]

I have a map of string to IO like this Map[String, IO[String]], I want to transform it into IO[Map[String, String]]. How to do it?
Kumar Waghmode
  • 509
  • 2
  • 18
11
votes
2 answers

IO monad prevents short circuiting of embedded mapM?

Somewhat mystified by the following code. In non-toy version of the problem I'm trying to do a monadic computation in a monad Result, the values of which can only be constructed from within IO. Seems like the magic behind IO makes such computations…
NioBium
  • 583
  • 3
  • 10
11
votes
1 answer

Why does GHCI get "stuck" in an error state after an error?

First of all, my apologies for the non-descriptive title. Since I have no idea what's actually going on I can't really make it any more specific. Now for my question. I have implemented the following snippet for problem 23 of the 99 Haskell…
Tiddo
  • 6,331
  • 6
  • 52
  • 85
11
votes
1 answer

What's going on in this type signature? (Vector.Mutable modifiers in Haskell)

Mutable vectors in Haskell have three element-level mutators: read :: PrimMonad m => MVector (PrimState m) a -> Int -> m a write :: PrimMonad m => MVector (PrimState m) a -> Int -> a -> m () swap :: PrimMonad m => MVector (PrimState m) a -> Int ->…
Justin L.
  • 13,510
  • 5
  • 48
  • 83
10
votes
1 answer

Scala Cats Effects - IO Async Shift - How Does it Work?

Here is some Scala cats code using the IO Monad: import java.util.concurrent.{ExecutorService, Executors} import cats.effect.IO import scala.concurrent.{ExecutionContext, ExecutionContextExecutor} import scala.util.control.NonFatal object Program…
Michael Lafayette
  • 2,972
  • 3
  • 20
  • 54
9
votes
1 answer

What are Tower[A] and IvoryTower in Scalaz?

When I looked at scalaz.effect.IO source code, I noticed that it has a method apply with the following signature: sealed trait IO[A] { def apply(rw: Tower[IvoryTower]): Trampoline[(Tower[IvoryTower], A)] } Tower[A] and IvoryTower are defined…
ZhekaKozlov
  • 36,558
  • 20
  • 126
  • 155
9
votes
1 answer

Is it possible to do the IO monad from Haskell in Clojure?

I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges. The closest I can find is Konrad Hinsen's library Monadic IO streams - but this doesn't…
hawkeye
  • 34,745
  • 30
  • 150
  • 304
9
votes
5 answers

Stop threads from interleaving output

The following program creates two threads running concurrently, that each sleep for a random amount of time, before printing a line of text to stdout. import Control.Concurrent import Control.Monad import System.Random randomDelay t = randomRIO (0,…
Chris Taylor
  • 46,912
  • 15
  • 110
  • 154
8
votes
2 answers

Show for IO types

I have a data type which contains an IORef as an important element. This means there is not a clean way to make it a member of the show type class. This is not too bad as I have a print function in the IO monad for this type. But it is annoying in…
John F. Miller
  • 26,961
  • 10
  • 71
  • 121
1
2 3
12 13