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
1
vote
5 answers

Haskell: how to keep count

I'm walking a directory recursively, in a conventional way. This is a working prototype: traverseFlatDst :: FilePath -> Int -> Int -> FilePath -> IO () traverseFlatDst dstRoot total totw srcDir = do (dirs, files) <- listDir srcDir mapM_ (\file…
Alexey Orlov
  • 2,412
  • 3
  • 27
  • 46
1
vote
1 answer

Haskell Put IO String

I am trying to read and print the output from the "readProcess" command mapped onto a list of filenames: files <- readProcess "ls" [] [] let mdList = map ( \file -> do md <- readProcess "mdls" [file] [] return md ) $ splitOn "\n" files in …
genghiskhan
  • 1,095
  • 12
  • 21
1
vote
2 answers

Haskell - how to avoid messing pure with IO

I am implementing some algorithm on haskell. This algorithm requires generating some data. I have a function of an algorithm which takes generation function as a parameter. For example, algorithm is just multiplying input data by n: algo :: a ->…
Rahul
  • 1,727
  • 3
  • 18
  • 33
1
vote
1 answer

Idiomatic way to implement "m (t a) -> (a -> m (t b)) -> m (t b)"

The bind function (>>=) has the signature: m a -> (a -> m b) -> m b However, I want a function with the signature: m (t a) -> (a -> m (t b)) -> m (t b) Specifically, I have a function that given an Integer, it returns a list of integers in within…
mandark
  • 762
  • 5
  • 21
1
vote
1 answer

Write to multiple files in Haskell

In Haskell, how would one go about writing to an arbitrarily large number of files? As an example, let's say I want to take the letters a through z and put them in files named for the letter of their contents. An initial attempt was to do the…
martin
  • 1,102
  • 2
  • 12
  • 20
1
vote
2 answers

What does a "monadic structure" and "element of a structure" precisely mean in the context of arbitrary Monad?

Reading through the documentation of Control.Monad I found such description of mapM: Map each element of a structure to a monadic action, evaluate these actions from left to right, and collect the results I'm interested, what does “element of a…
MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102
1
vote
1 answer

Haskell Safe IO

I am attempting to write a simple function to safely read a file (if it exists) and do nothing if the file does not exist: safeRead :: String -> IO () safeRead path = readFile path `catch` handleExists where handleExists e |…
Abraham P
  • 15,029
  • 13
  • 58
  • 126
1
vote
1 answer

Haskell: Couldn't match expected type 'IO b' with actual type bool

I get the following error: Couldn't match expected type `IO b' with actual type `Bool' In the expression: upCheck ["########", "#11xxx3#", "#xx2xx3", "#002xx3", ....] 2 [2, 3, 4] In the expression: do { s <- readFile "projekt-board.txt"; …
Banana
  • 814
  • 1
  • 8
  • 28
1
vote
1 answer

Run this monadic computation with notion of state and randomness

I describe the following computation: import Control.Monad.State import Control.Monad.Identity import Control.Monad.Random.Class -- * fair coin fair :: MonadRandom m => m Bool fair = (\p -> p <= 0.5) <$> getRandomR (0,1 :: Double) -- * how do i…
xiaolingxiao
  • 4,793
  • 5
  • 41
  • 88
1
vote
2 answers

Haskell. Why it is working? (Iteration over Monad)

liftMM :: Monad m => (a -> b) -> m a -> m b liftMM f m = m >>= \a -> return (f a) I run: > liftMM (*2) [1..10] I got output: > [2,4,6,8,10,12,14,16,18,20] I can not see how this function is mapping over all list's values? There is no any…
Andrew
  • 36,676
  • 11
  • 141
  • 113
1
vote
1 answer

read/write another process memory monad in F#

I work on cheat for a single player game. I like function composition, immutability and a code without boilerplate so that's why I decided to write the cheat in F#. I finished something which works fine. I know that the code is far from perfect, but…
1
vote
1 answer

Combining Scalaz IO monad with Stream for simple echo program using takeWhile method

I'm looking for a solution for "Life, the Universe, and Everything" problem on spoj.com using Scalaz IO Monad and Stream. The problem is to rewrite small numbers from input to output and to stop processing input after reading in the number 42. I…
ralv
  • 46
  • 5
1
vote
3 answers

Reading numbers inline

Imagine I read an input block via stdin that looks like this: 3 12 16 19 The first number is the number of following rows. I have to process these numbers via a function and report the results separated by a space. So I wrote this main…
Hennes
  • 1,340
  • 1
  • 10
  • 26
1
vote
2 answers

How to convert IO Int to String in Haskell?

I'm learning to use input and output in Haskell. I'm trying to generate a random number and output it to another file. The problem is that the random number seems to be returning an IO Int, something that I can't convert to a String using…
JDL Wahaha
  • 695
  • 1
  • 14
  • 22
0
votes
1 answer

Expression Evaluation (Add, Mult,etc) with Cats-Effect

I am new to cats-effect and I am trying to implement the classical expression evaluation using cats-effect. Using eval I would like to return an IO[Double] instead of Double. I have my naive code below but of course it doesnt type check. What is the…