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
8
votes
4 answers

Guess My Number, a monadic headache

To test my skills in Haskell, I decided I wanted to implement the very first game you find in Land of Lisp / Realm of Racket. The "Guess My Number" game. The game relies on mutable state to run, as it constantly has to update upper and lower bounds…
Electric Coffee
  • 11,733
  • 9
  • 70
  • 131
7
votes
3 answers

Is print in Haskell a pure function?

Is print in Haskell a pure function; why or why not? I'm thinking it's not, because it does not always return the same value as pure functions should.
bubu
  • 179
  • 2
  • 13
7
votes
2 answers

Idiomatic Haskell syntax without do-blocks or accumulating parentheses?

I'm fairly new to Haskell and have been trying to find a way to pass multiple IO-tainted values to a function to deal with a C library. Most people seem to use the <- operator inside a do block, like this: g x y = x ++ y interactiveConcat1 = do {x…
user4861515
7
votes
4 answers

Refactoring “staircasing” with case of `Maybe` values in `IO` code

The following function f attempts to read an Int twice by using an IO (Maybe Int) function twice, but “short-circuits” execution after successfully reading one Int: readInt :: IO (Maybe Int) f :: IO (Maybe Int) f = do n1 <- readInt case n1 of …
beta
  • 2,380
  • 21
  • 38
7
votes
2 answers

How do I use map over a list with do notation - ie avoid type `IO ()' with type `[IO ()]'?

So this question is about Monads more generally (in particuar for Fay), but my example uses the IO monad. I have a function where the input is a list of strings and I would like to print each string one by one. So here was my idea: funct ::…
aled1027
  • 1,326
  • 2
  • 13
  • 17
6
votes
1 answer

Is there any way to lift content from IO to other container without run?

According to the cats official document: https://typelevel.org/cats-effect/typeclasses/liftio.html, if we want lift something from IO to other container, you should implement LiftIO trait, but the example explicitly run unsafeRunXXX method to fetch…
6
votes
2 answers

What is the IO Haskell Monad equivalent in Scala standard API?

I know there are some almost identical implementations in Scalaz of IO operators like putStrLn :: String -> IO () and getLine :: IO String but I mean about Scala standard API why there aren't such equivalents? I know Scala is not a pure language as…
salc2
  • 577
  • 5
  • 14
6
votes
1 answer

How to construct a no-op IO() expression in Haskell?

Here's an excerpt of a domain-specific file-IO function I'm writing: let cp :: FilePath -> IO () cp "." = putStr "" -- OUCH! cp ".." = putStr "" -- CRIKEY! cp fname = custom logic here... in mapM_ cp filepaths I understand mapM_…
metaleap
  • 2,132
  • 2
  • 22
  • 40
6
votes
2 answers

In what sense is IO monad special (if at all)?

After diving into monads I understand that they are a general concept to allow chaining computations inside some context (failing, non-determinism, state, etc) and there is no magic behind them. Still IO monad feels even if not magic, but special.…
Konstantin Milyutin
  • 11,946
  • 11
  • 59
  • 85
6
votes
1 answer

Dynamically generate Tasty `TestTree` from the file system

I have written a file parser using the Parsec library. I would like to write a high-level unit test using the Tasty testing framework to ensure that the parser correctly parses some given files. I have three well formatted files in the following…
recursion.ninja
  • 5,377
  • 7
  • 46
  • 78
5
votes
3 answers

Why is there difference between throw and throwIO?

I am trying to get a firm grasp of exceptions, so that I can improve my conditional loop implementation. To this end, I am staging various experiments, throwing stuff and seeing what gets caught. This one surprises me to no end: % cat X.hs module…
Ignat Insarov
  • 4,660
  • 18
  • 37
5
votes
1 answer

Why isn't this this applicative statement being lazily evaluated, and how can I understand why?

abc :: IO (Int) abc = do print "abc" pure $ 10 xyz :: IO (Int) xyz = undefined main :: IO () main = do x <- (((+) <$> abc <*> abc) <* xyz) print x Why in the above is xyz being evaluated? I would assume due to Haskell's lazy nature it…
Chris Stryczynski
  • 30,145
  • 48
  • 175
  • 286
5
votes
2 answers

Mocking of BlazeClientBuilder[IO] to return mock client[IO]

I am using the BlazeClientBuilder[IO].resource method to get Client[IO]. Now, I want to mock the client for unit testing but cannot figure out how to do so. Is there a good way of mocking this and how would I do that? class ExternalCall(val…
Kumar Waghmode
  • 509
  • 2
  • 18
5
votes
1 answer

Haskell: [IO ()] to IO ()

Haskell wiki has the following question: https://en.wikibooks.org/wiki/Haskell/Higher-order_functions for :: a -> (a -> Bool) -> (a -> a) -> (a -> IO ()) -> IO () for i p f job = -- ??? I was able to come up with the following…
coder_bro
  • 10,503
  • 13
  • 56
  • 88
5
votes
1 answer

How can I use an `IO String` inside a Spock request handler?

I have the following function that produces a random string of characters in multiples of 1024: import System.Random rchars :: Int -> [IO Char] rchars n = map (\_ -> randomRIO ('a', 'z')) [n | n <- [0..n]] -- a wasteful "iteration"-like func rstr…
Alex
  • 8,093
  • 6
  • 49
  • 79
1
2
3
12 13