io-monad

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

有些话、适合烂在心里 提交于 2021-02-04 03:18:28
问题 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 would force the first argument of second to be evaluated, and the >>= operator would need to run print "hi" in order to get a value off of it and pass it to \r -> return () , which would print "hi" to the screen. What's wrong with my reasoning? And

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

人走茶凉 提交于 2021-02-04 03:05:14
问题 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 would force the first argument of second to be evaluated, and the >>= operator would need to run print "hi" in order to get a value off of it and pass it to \r -> return () , which would print "hi" to the screen. What's wrong with my reasoning? And

How monadicIO works

痞子三分冷 提交于 2020-03-22 09:03:11
问题 I have the following code fastShuffle :: [a] -> IO [a] fastShuffle a = <some code> prop_fastShuffle_correct :: [Int] -> Property prop_fastShuffle_correct s = monadicIO ( do sh <- run (fastShuffle s) return ( True ==> ( insertionSort sh == insertionSort s && if length s > 10 then s /= sh else True ) ) ) And .. it is working. I can't understand how what looks to be a pure function ( prop_fastShuffle_correct ) can call a non pure function that has side effects ( fastShuffle ). Hope that someone

Monad and MonadIO for custom type

♀尐吖头ヾ 提交于 2020-01-06 14:16:05
问题 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 Control.Monad import System.IO import Control.Monad.IO.Class instance Functor Logger where fmap = liftM instance Applicative Logger where pure = return (<*>) = ap newtype Logger a = Logger a deriving (Show) instance Monad (Logger) where return = Logger Logger logStr

Haskell — dual personality IO / ST monad?

微笑、不失礼 提交于 2020-01-02 00:12:09
问题 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 has gotten longer, I do want to put debugging print statements in. Is there any class that provides a dual-personality monad [or typeclass machinery], one which can be a ST or an IO (depending on its type or a "isDebug" flag)? I recall SPJ

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

杀马特。学长 韩版系。学妹 提交于 2020-01-01 08:45:59
问题 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 -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read

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

拈花ヽ惹草 提交于 2020-01-01 08:45:27
问题 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 -> Int -> m () Now I can use these fine -- import Data.Vector import Data.Vector.Mutable import Control.Monad.ST import Control.Monad.Primitive incrAt :: Vector Double -> Int -> Vector Double incrAt vec i = runST $ do mvec <- thaw vec oldval <- read

Unwrap value from IO operation at a later time

安稳与你 提交于 2019-12-24 12:02:12
问题 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 result of the evaluation? Code let inh=openFile "myfile" WriteMode let outh=openFile "out.txt" WriteMode hPutStrLn (<-outh) ((<-inh)>>=getLine) I have not entered the Monad chapter just basic <- and do blocks but i suppose it has to do with monads.

fmap into a do block fails with a print error

大城市里の小女人 提交于 2019-12-24 07:19:24
问题 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 1 Produces 1 But -- This fails fmap (\x -> do let m = T.pack $ show x T.putStrLn m ) [1..10] -- And this also fails fmap (\x -> do T.putStrLn $ T.pack $ show x ) [1..10] With error: <interactive>:1:1: error: • No instance for (Show (IO ())) arising from a use of