monads

transforming IO String to String

℡╲_俬逩灬. 提交于 2019-12-20 07:16:42
问题 I am having an issue converting IO String() to a String() Below is the function to eval an expression. foobar :: String -> IO String eval :: String -> Sh () () eval x = do s <- foobar x shellPutStrLn $ s This isnt working, because eval returns IO String instead of Sh. The moment some IO is done within a function it transforms/taints it into IO String. How do I prevent the transformation or convert an IO String to String ? 回答1: It looks like your Sh type is supposed to be able to do IO. The

Is it safe to derive MonadThrow, MonadCatch, MonadBaseControl, MonadUnliftIO, etc?

和自甴很熟 提交于 2019-12-20 04:31:07
问题 I'm refactoring some old code, which is in a polymorphic, but type-class constrained, monad: class ( MonadIO m , MonadLogger m , MonadLoggerIO m , MonadThrow m , MonadCatch m , MonadMask m , MonadBaseControl IO m , MonadUnliftIO) => HasLogging m where In the older code the application's main monad was... type AppM = ReaderT Env IO ...which will now change to... newtype AppM (features :: [FeatureFlag]) a = AppM (ReaderT Env IO a) deriving (Functor, Applicative, Monad, MonadReader Env, MonadIO)

Dispatching to correct function with command line arguments in Haskell

瘦欲@ 提交于 2019-12-20 04:23:39
问题 I'm writing a little command-line program in Haskell. I need it to dispatch to the correct encryption function based on the command line arguments. I've gotten that far, but then I need the remaining arguments to get passed to the function as parameters. I've read: http://learnyouahaskell.com/input-and-output That's gotten me this far: import qualified CaesarCiphers import qualified ExptCiphers dispatch::[(String, String->IO ())] dispatch = [("EEncipher", ExptCiphers.exptEncipherString) (

How can I write this simple code using the state monad?

 ̄綄美尐妖づ 提交于 2019-12-20 03:22:05
问题 I'm a beginner at Haskell and I've come across a situation where I would like to use the state monad. (Or at least, I think I that's what I'd like to use.) There are a million tutorials for the state monad, but all of them seem to assume that my main goal is to understand it on a deep conceptual level, and consequently they stop just before the part where they say how to actually develop software with it. So I'm looking for help with a simplified practical example. Below is a very simple

Practical Implications of runST vs unsafePerformIO

馋奶兔 提交于 2019-12-19 17:36:08
问题 I want something like f :: [forall m. (Mutable v) (PrimState m) r -> m ()] -> v r -> v r -- illegal signature f gs x = runST $ do y <- thaw x foldM_ (\_ g -> g y) undefined gs -- you get the idea unsafeFreeze y I'm essentially in the same position I was in this question where Vitus commented: [I]f you want keep polymorphic functions inside some structure, you need either specialized data type (e.g. newtype I = I (forall a. a -> a)) or ImpredicativeTypes. Also, see this question. The problem

How does this State monad code works?

荒凉一梦 提交于 2019-12-19 08:58:49
问题 This code is from this article I've been able to follow it until this part. module Test where type State = Int data ST a = S (State -> (a, State)) apply :: ST a -> State -> (a,State) apply (S f) x = f x fresh = S (\n -> (n, n+1)) instance Monad ST where -- return :: a -> ST a return x = S (\s -> (x,s)) -- (>>=) :: ST a -> (a -> ST b) -> ST b st >>= f = S (\s -> let (x,s') = apply st s in apply (f x) s') data Tree a = Leaf a | Node (Tree a) (Tree a) deriving (Show) mlabel :: Tree a -> ST (Tree

Is there a standard option workflow in F#?

喜夏-厌秋 提交于 2019-12-19 05:03:28
问题 Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. 回答1: There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number

Is there a standard option workflow in F#?

烂漫一生 提交于 2019-12-19 05:03:04
问题 Is there an option (maybe) wokflow (monad) in the standrd F# library? I've found a dozen of hand-made implementations (1, 2) of this workflow, but I don't really want to introduce non-standard and not very trusted code into my project. And all imaginable queries to google and msdn gave me no clue where to find it. 回答1: There's no Maybe monad in the standard F# library. You may want to look at FSharpx, a F# extension written by highly-qualified members of F# community, which has quite a number

Monad more powerful than Applicative?

谁都会走 提交于 2019-12-19 05:00:50
问题 I looked at past discussion but could not see why any of the answers are actually correct. Applicative <*> :: f (a -> b) -> f a -> f b Monad (>>=) :: m a -> (a -> m b) -> m b So if I get it right, the claim is that >>= cannot be written by only assuming the existence of <*> Well, let's assume I have <*> . And I want to create >>= . So I have f a . I have f (a -> b) . Now when you look at it, f (a -> b) can be written as (a -> b) (if something is a function of x, y , z - then it's also a

Is it possible to do the Reader Monad from Haskell in Clojure?

别等时光非礼了梦想. 提交于 2019-12-19 02:32:40
问题 I've had a look at the algo.monads and fluokitten documentation. I've also read through monad blog entries by Jim Duey, Konrad Hinsen and Leonardo Borges. The only reference I can find to the Reader Monad in Clojure is this google groups discussion. My question is: Is it possible to do the Reader Monad from Haskell in Clojure? Could you provide an example? 回答1: Sure. A Reader is just a function that takes an environment and extracts some value from it. With Reader , m-result takes some value