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
2 answers

How do I QuickCheck a Servant Application that is constructed from an IO?

I am writing an API server using Servant. The server includes persistent state. I would like to use QuickCheck to write tests for the server. The implementation of various endpoints that make up the Servant Application require a database value. …
Jean-Paul Calderone
  • 47,755
  • 6
  • 94
  • 122
1
vote
1 answer

How can i use MVars to move paddles on my pingpong haskell game?

I already have a function that moves 2 paddles in a ping pong game in haskell. I want to change so it uses MVars now. I know that i need to change wHeld, sHeld, downHeld and upHeld to MVars but any ideas on how to change movePaddle to deal with…
1
vote
3 answers

Random grid of elements using Haskell

Having absolutely zero experience with Haskell, I need to come up with a code equivalent to this Python one: from random import choice, sample def random_subset(): return tuple(sample(('N', 'S', 'W', 'E'), choice((1, 2, 3, 4)))) def…
Enirsa
  • 85
  • 1
  • 7
1
vote
1 answer

Couldn't match expected type ‘IO [String]’ with actual type ‘[String]’

I have these two code snippets, which I'd guess do the same thing, but they don't. Why is that? This one works fine: fdup :: String -> IO () fdup filename = do h <- openFile filename ReadMode c <- hGetContents h putStr $…
T.Poe
  • 1,949
  • 6
  • 28
  • 59
1
vote
2 answers

fmap into a do block fails with a print error

I'm trying to understand why a function I have written with a do-block can't be rewritten to fmap a similar lambda expression over a list. I have the following: -- This works test1 x = do let m = T.pack $ show x T.putStrLn m test1…
Mittenchops
  • 18,633
  • 33
  • 128
  • 246
1
vote
1 answer

Can I have separate functions for reading and writing to a txt file in Haskell, without using a 'main' function?

I'm making a program using Haskell that requires simple save and load functions. When I call the save function, I need to put a string into a text file. When I call load, I need to pull the string out of the text file. I'm aware of the complexities…
SuperHanz98
  • 2,090
  • 2
  • 16
  • 33
1
vote
2 answers

Ask user for list input in Haskell

I found this code online, but it's not running. main = do xs <- getLine [] print xs So how do I ask the user for list input in Haskell? I am new to Haskell, please explain when you answer. Thanks.
user11239824
1
vote
1 answer

Can not return maybe result in the IO Monad

I do not understand why this sample of code does not work and in RWH book it works: module Monads where import Data.Maybe import Control.Monad amap=[("a",1),("bb",2)] bmap=[(1,100),(2,200)] final=[(100,1000),(200,2000)] …
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
2 answers

A mystery involving putStrLn

Why does the piece of code below produce the error parse error on input ‘putStrLn’? main = do line <- fmap reverse getLine putStrLn $ "You said " ++ line ++ " backwards!" putStrLn $ "Yes, you said " ++ line ++ "…
user65526
  • 685
  • 6
  • 19
1
vote
1 answer

Can IO action in negative position give unexpected results?

There seems to be some undocumented knowledge about the difference between Monad IO and IO. Remarks here and here) hint that IO a can be used in negative position but may have unintended consequences: Citing Snoyman 1: However, we know that some…
sevo
  • 4,559
  • 1
  • 15
  • 31
1
vote
1 answer

Monad and MonadIO for custom type

I have a Logger type of kind * -> * which can take any type and log the value in a file. I am trying to implement this in a monadic way so that I log and keep working the same. My code looks like import Control.Applicative import…
DBS
  • 794
  • 2
  • 9
  • 21
1
vote
2 answers

How to specialize mapM for IO in Haskell

Say I have a task that represents some computation from k to v where some inputs have to be fetched externally. newtype Task k v = Task { run ∷ ∀ f. Monad f ⇒ (k → f v) → f v } For some tasks mapM will be used, e.g. to fetch multiple keys. I want…
Chris
  • 953
  • 11
  • 16
1
vote
1 answer

Unwrap value from IO operation at a later time

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…
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
1
vote
0 answers

Bracket typeclass in cats-effect 0.10.1

I'm using cats-effect 0.10.1 and found out that there is no Bracket typeclass in this version. So this seems I need to introduce some workaround for that. I'm designing function for Reading from some source (side-effect). So I have \ trait…
Some Name
  • 8,555
  • 5
  • 27
  • 77
1
vote
0 answers

cats-effects fibers autocancellation

im quite new to cats-effects and im already loving and enjoying it :) this is about fibers as i saw that fibers can be cancelled im trying to see if its possible to have an IO with a list of fibers that, upon an error on any of the fibers, can…