io-monad

Portably opening a handle to stdin many times in a single session

对着背影说爱祢 提交于 2019-12-24 00:58:58
问题 Code: main = do putStrLn "4917 Microprocessor\nEnter the Machine Code to be run: " inp <- getContents putStrLn "The output of the Program is:" fState <- ((runStateT _4917) . construct . parse) inp args <- getArgs if elem "-v" args then putStrLn ("\nFinal state was: " ++ (show . snd) fState) else return () putStrLn "\n================================ RESTART ================================" main where parse xs = array (0,15) $ zip [0..15] $ take 16 $ map ((makeArbInt 4) . read) (words (filter

Yesod's shakespearean templates (hamlet) and IO

痞子三分冷 提交于 2019-12-20 02:28:32
问题 In Hamlet, how does one uses the result of an IO operation inside #{...} ? For instance : someIO :: IO String ----------------- $with stuff <- someIO <p>#{stuff} Fails with No instance for (blaze-markup-0.6.0.0:Text.Blaze.ToMarkup (IO String)) arising from a use of `toHtml' I fear that I have not approached the problem from the right angle, could someone shed some light on this issue for me? Thank you 回答1: Hamlet is just providing an alternate syntax for normal Haskell code, so like normal

Combining and splitting assignment in Haskell IO do block

北战南征 提交于 2019-12-13 07:09:27
问题 I /think/ I have a similar misunderstanding of the language in two places involving how variable assignment works in do blocks, involving the IO monad. Could you help me understand (1) is it the same misunderstanding, (2) how to clear it up (in an answer, and maybe specifically if you have a favorite reference on the subject)? I find that I can perform an operation successfully when it is all one line, but not when I try to split into 2 for readability. Part I: Turning 1 line into 2 Why does

IO action nested in other monads not executing

强颜欢笑 提交于 2019-12-12 01:46:39
问题 I have a foobar :: IO (ParseResult [(String,String)]) ParseResult is a monad defined here: https://hackage.haskell.org/package/haskell-src-exts-1.13.5/docs/Language-Haskell-Exts-Parser.html#t:ParseResult I want to take those strings and write them to a LaTeXT m () defined in https://hackage.haskell.org/package/HaTeX-3.17.1.0/docs/Text-LaTeX-Base-Writer.html Running this function results in no file being created. writeReport2 :: [Char] -> IO (ParseResult (IO ())) writeReport2 name = do x <-

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

若如初见. 提交于 2019-12-11 17:21:20
问题 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 MVars? Also when i declare wHeld an MVars it shows a error on deriving show (Non instance for (Show MVar Bool)) data PongGame = Game { ballLoc :: (Float, Float) -- ^ Pong ball (x, y) location. , ballVel :: (Float, Float) -- ^ Pong ball (x, y) velocity.

How do I make a Scalaz ZIO lazy?

 ̄綄美尐妖づ 提交于 2019-12-10 19:56:19
问题 I have a heavy side-effecting function (think database call) that I want to use as a lazy value, so that it gets called only on first use (and not at all if never used). How do I do this with ZIO? If my program looks like this, the function gets called only once (but even the result is not used at all): import scalaz.zio.IO import scalaz.zio.console._ object Main extends scalaz.zio.App { def longRunningDbAction: IO[Nothing, Integer] = for { _ <- putStrLn("Calling the database now") } yield 42

Is there a way to unwrap a type from an IO monad?

蹲街弑〆低调 提交于 2019-12-10 18:09:02
问题 I have this very simple function import qualified Data.ByteString.Lazy as B getJson :: IO B.ByteString getJson = B.readFile jsonFile readJFile :: IO (Maybe Response) readJFile = parsing >>= (\d -> case d of Left err -> return Nothing Right ps -> return (Just ps)) where parsing = fmap eitherDecode getJson :: IO (Either String Response) where jsonFile is a path to a file on my harddrive (pardon the lack of do-notation, but I found this more clear to work with) my question is; is there a way for

Is print in Haskell a pure function?

让人想犯罪 __ 提交于 2019-12-07 00:31:43
问题 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. 回答1: A value of type IO Int is not really an Int . It's more like a piece of paper which reads "hey Haskell runtime, please produce an Int value in such and such way". The piece of paper is inert and remains the same, even if the Int s eventually produced by the runtime are different. You send the piece of paper to the runtime by assigning it

Is print in Haskell a pure function?

ぐ巨炮叔叔 提交于 2019-12-05 04:16:16
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. danidiaz A value of type IO Int is not really an Int . It's more like a piece of paper which reads "hey Haskell runtime, please produce an Int value in such and such way". The piece of paper is inert and remains the same, even if the Int s eventually produced by the runtime are different. You send the piece of paper to the runtime by assigning it to main . If the IO action never comes in the way of main and instead languishes inside some

Haskell — dual personality IO / ST monad?

丶灬走出姿态 提交于 2019-12-04 22:42:31
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 introduced a "Mutation" class in his "Fun with Type Functions" paper, which used associative types to relate IO