monads

MonadFix in strict language

喜你入骨 提交于 2019-12-21 08:17:23
问题 I'm working on camlp4 extension for haskell-like do notation in Ocaml, and trying to figure out how GHC compiles recursive do-bindings (enabled with -XDoRec). I wonder if it possible for monadic fixpoint combinator to exist in strict language (like Ocaml/F#/SML/...)? If yes, how can it look like? Would it be very useful? 回答1: The F# computation expression syntax (related to Haskell do ) supports recursion: let rec ones = seq { yield 1 yield! ones } This is supported because the computation

Permutations of a list - Haskell

三世轮回 提交于 2019-12-21 07:23:13
问题 I want to make all possible combinations of subgroups with 2 lists. Here is a function that does just this: getCombinations :: [a] -> [[a]] getCombinations na = do a <- na b <- na [[a,b]] If you pass "abc" to this function, it returns this: ["aa","ab","ac","ba","bb","bc","ca","cb","cc"] A simple modification of the same method could return combinations of 3 lists instead of two. getCombinations :: [a] -> [[a]] getCombinations na = do a <- na b <- na c <- na [[a,b,c]] Result of passing "abc"

Why there is no `Cofunctor` typeclass in Haskell?

我只是一个虾纸丫 提交于 2019-12-21 07:19:21
问题 Monads get fmap from Functor typeclass. Why comonads don't need a cofmap method defined in a Cofunctor class? 回答1: Functor is defined as: class Functor f where fmap :: (a -> b) -> (f a -> f b) Cofunctor could be defined as follows: class Cofunctor f where cofmap :: (b -> a) -> (f b -> f a) So, both are technically the same, and that's why Cofunctor does not exist. "The dual concept of 'functor in general' is still 'functor in general'". Since Functor and Cofunctor are the same, both monads

What are all of the monad naming conventions?

断了今生、忘了曾经 提交于 2019-12-21 07:12:40
问题 It seems that Haskell has established several naming conventions around monads. Examples: appending T to the end to obtain the name of the monad transformer (e.g. Reader -> ReaderT ) using runXXX to perform a monad computation (e.g. runST , runReader ) liftXXX for various values of XXX Are there other naming conventions? 回答1: runX m where m :: X a will run the X monad and return the "side effect" along with the monad result, a . evalX m will run the computation and return the result, a .

Why isn't `join` part of the `Monad` class [duplicate]

≡放荡痞女 提交于 2019-12-21 07:04:42
问题 This question already has answers here : Why is join standalone, instead of part of the minimal implementation of the Monad typeclass? (3 answers) Closed last year . It is a well known fact that (>>=) can be implemented using fmap and join while join can be implemented using >>= . Is there any reason we don't define the Monad class with join included and using the following default definitions? join x = x >>= id x >>= f = join $ f <$> x This would allow a minimal definition to include either

The “reader” monad

妖精的绣舞 提交于 2019-12-21 06:58:33
问题 OK, so the writer monad allows you to write stuff to [usually] some kind of container, and get that container back at the end. In most implementations, the "container" can actually be any monoid. Now, there is also a "reader" monad. This, you might think , would offer the dual operation - incrementally reading from some kind of container, one item at a time. In fact, this is not the functionality that the usual reader monad provides. (Instead, it merely offers easy access to a semi-global

Stop for-comprehension mid-flow when using stacked monads of State and IO

狂风中的少年 提交于 2019-12-21 05:46:13
问题 In this Scala example I need to stop when the result is StopNow , I need to do this after calling decisionStep . How can I do that? case class BusinessState() trait BusinessResult case object KeepGoing extends BusinessResult case object StopNow extends BusinessResult type IOState[S, A] = StateT[IO, S, A] type BusinessIOState[A] = IOState[BusinessState, A] trait SomeSteps { def step1:BusinessIOState[Unit] def step2:BusinessIOState[BusinessState] def decisionStep:BusinessIOState[BusinessResult]

Use StateT within Web.Scotty

ε祈祈猫儿з 提交于 2019-12-21 04:27:17
问题 I'm trying to make a silly webserver that stores data as State . I'm using Web.Scotty. I've used ReaderT before with scotty to access config, but following the same approach doesn't work here. It resets the state on every request. I want to set the initial state when the program starts, then have that same state stick around for the whole life of the program. How can I make this work? (The following creates a new state every request) {-# LANGUAGE OverloadedStrings #-} import Web.Scotty.Trans

Combining StateT and State monads

我只是一个虾纸丫 提交于 2019-12-21 03:26:15
问题 Lets say I have a function f :: State [Int] Int and a function: g :: StateT [Int] IO Int I want to use f in g and pass the state between them. Is there a library function for StateT (return . runState f) ? Or in general, given a monad transformer with a corresponding monad, is there a library function for it? 回答1: In even more general, what you're trying to do is apply a transformation to an inner layer of a transformer stack. For two arbitrary monads, the type signature might look something

Using servant with ReaderT IO a

人走茶凉 提交于 2019-12-20 21:02:25
问题 I'm using the servant library for my JSON API. I need some help to get a ServerT MyAPI (ReaderT a IO) monad stack working. Here's an example using ReaderT , but without integrating it with servant: -- this code works type TestAPI = "a" :> Get '[JSON] String :<|> "b" :> Get '[JSON] String test2 :: EitherT ServantErr IO String test2 = return "asdf" testServer :: Int -> Server TestAPI testServer code = test :<|> test2 where test :: EitherT ServantErr IO String test = liftIO $ runReaderT