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
0
votes
1 answer

How do you run a single test with QuickCheck when using monadicIO in Haskell?

I am using QuickCheck as a test suite in Haskell. I want to run a single test that is always the same in the IO Monad. The problem is that QuickCheck generates 100 tests automatically even though the test does not take any parameters. As an example…
Terra
  • 100
  • 1
  • 8
0
votes
1 answer

Infinite loop when trying to read from Java Scanner inside an IO Monad

I was trying to do an exercise for a Cats Effect course in which I was required, among other things, to read a document line by line and wait a certain amount of time between lines. The teacher's solution was the following: def readLines(sc:…
0
votes
1 answer

How would a functional language actually define/translate primitives to hardware?

Let's say I have a few primitives defined, here using javascript: const TRUE = x => y => x; const FALSE = x => y => y; const ZERO = f => a => a; const ONE = f => a => f(a); const TWO = f => a => f(f(a)); If a language is purely function, how would…
David542
  • 104,438
  • 178
  • 489
  • 842
0
votes
1 answer

Trying to use an Int RandomIO val in my chart

I am new to monads in Haskell. I was trying to write a scatter graph with Haskell-chart that would plot 20 points with strong (but not perfect) positive correlation. Example of what I was trying to do. Specifically, I want a list of random doubles…
m1531
  • 11
  • 2
0
votes
2 answers

How to avoid the IO monad when solving arithmetic problems in SBV

I am trying to solve arithmetic problems with SBV. For example solution :: SymbolicT IO () solution = do [x, y] <- sFloats ["x", "y"] constrain $ x + y .<= 2 Main> s1 = sat solution Main> s2 = isSatisfiable solution Main> s1 Satisfiable.…
0
votes
1 answer

How to use pure functions recursively in types with IOMonad instances?

I get the error: Main.hs:38:22: error: • Couldn't match type ‘WD ()’ with ‘()’ ... • In the expression: setScrollHPos height >> scrollUntilEnd0 height <$> getHeight where: setScrollHPos :: Integer -> WD () scrollUntilEnd0 :: Integer…
Delfin
  • 157
  • 7
0
votes
1 answer

Apparent redundant calls in a IO monad?

Here is a snippet of code taken from the Haskell GPipe project (commented by myself, save the line with "Really?"). In the memoize function, I don't understand why its author call the getter a second time to cache a newly computed value. It doesn't…
Chatanga
  • 77
  • 2
0
votes
1 answer

Exception handling and purity in Haskell

In The acquire-use-release cycle section from Real World Haskell, the type of bracket is shown: ghci> :type bracket bracket :: IO a -> (a -> IO b) -> (a -> IO c) -> IO c Now, from the description of bracket, I understand that an exception might…
Enlico
  • 23,259
  • 6
  • 48
  • 102
0
votes
1 answer

How can I implement this kind of IO action?

I have the following datatype, which encodes arbitrary real numbers: newtype ArbReal = ArbReal {approximate :: Word -> Integer} I have the following function, which finds the limit of a converging sequence of real numbers: inexactToReal :: (Word ->…
Dannyu NDos
  • 2,458
  • 13
  • 32
0
votes
2 answers

Are there any languages that handle functional impurity (side effects) without modeling them as RealWorld or IO?

One thing that always bothered me in Haskell (and other functional languages, for that matter) is that the entire language is pure, but side-effects are indirectly allowed by using an object that represents the entire "real world" (the IO monad, for…
user3F31A28
  • 104
  • 2
  • 13
0
votes
1 answer

"Couldn't match type ‘[]’ with ‘IO’" error in Haskell

I am trying to make a list with Points (a datatype that I created), the idea is add an element in each iteration. Something is wrong. I have tried to put p out of myLoop but it doesn't seems to work either. main = myLoop myLoop = do …
0
votes
1 answer

How to use randomness in Haskell to produce instances of a JSON "model"?

I have this work where I have to read a JSON from a file and generate instances of it based on its model. I'm using aeson to serialize the objects, but I'm having a huge problem dealing with randomness to produce new objects. Produce a new JSON…
Augusto Dias
  • 169
  • 1
  • 9
0
votes
1 answer

How does `return` statement have different type than that of a function's definition?

Within a loop, integers are collected inside a list, and a tuple of these integers is returned. How does this change to a list of tuples? input :: IO [(Int,Int)] input = do n <- readLn :: IO Int forM [1..n] $ \_ -> do [x,y] <- map read .…
Ndosho Mbwana
  • 31
  • 1
  • 1
  • 5
0
votes
1 answer

Haskell How to compare IO tuple with normal tuple

I try to compare the tuple members (date) of an IO tuple with a normal tuple. d1 ->(Integer, Int, Int) and d2 -> IO (Integer, Int, Int), Is it possible to compare these two tuples? I've tried something like that: import Data.Time.Clock import…
Habebit
  • 957
  • 6
  • 23
0
votes
2 answers

if statement in do block gives error message

I'm trying to make a very simple snake-like game where, if you try to go to a x,y coordinate you have already visited, you lose the game. This is code that's working so far (you can move player 1 with arrowkeys and player 2 with wasd): import…
abc556
  • 37
  • 5