monads

How does C# async/await relates to more general constructs, e.g. F# workflows or monads?

不羁岁月 提交于 2019-12-02 14:30:02
The C# language design have always (historically) been geared towards solving specific problems rather then finding to address the underlying general problems: see for example http://blogs.msdn.com/b/ericlippert/archive/2009/07/09/iterator-blocks-part-one.aspx for "IEnumerable vs. coroutines": We could have made it much more general. Our iterator blocks can be seen as a weak kind of coroutine. We could have chosen to implement full coroutines and just made iterator blocks a special case of coroutines. And of course, coroutines are in turn less general than first-class continuations; we could

Monad equivalent in Ruby

家住魔仙堡 提交于 2019-12-02 14:17:40
What would an equivalent construct of a monad be in Ruby? Apocalisp The precise technical definition : A monad, in Ruby, would be any class with bind and self.unit methods defined such that for all instances m: m.class.unit[a].bind[f] == f[a] m.bind[m.class.unit] == m m.bind[f].bind[g] == m.bind[lambda {|x| f[x].bind[g]}] Some practical examples A very simple example of a monad is the lazy Identity monad, which emulates lazy semantics in Ruby (a strict language): class Id def initialize(lam) @v = lam end def force @v[] end def self.unit lambda {|x| Id.new(lambda { x })} end def bind x = self

Avoiding lift with monad transformers

拟墨画扇 提交于 2019-12-02 14:03:12
I have a problem to which a stack of monad transformers (or even one monad transformer) over IO . Everything is good, except that using lift before every action is terribly annoying! I suspect there is really nothing to do about that, but I thought I'd ask anyway. I am aware of lifting entire blocks, but what if the code is really of mixed types? Would it not be nice if GHC threw in some syntactic sugar (for example, <-$ = <- lift )? For all the standard mtl monads, you don't need lift at all. get , put , ask , tell — they all work in any monad with the right transformer somewhere in the stack

Is do notation used necessarily in the context of monad?

蓝咒 提交于 2019-12-02 08:20:45
问题 Haskell report 2010 says A do expression provides a more conventional syntax for monadic programming . It allows an expression such as putStr "x: " >> getLine >>= \l -> return (words l) to be written in a more traditional way as: do putStr "x: " l <- getLine return (words l) Haskell the Craft of Functional programming by Thompson says We'll continue to use the do notation, but will keep in mind that it essentially boils down to the existence of a function (>>=) which does the work of

Can I use different workflows simultaneously in F#?

风流意气都作罢 提交于 2019-12-02 07:18:17
I need my state to be passed along while being able to chain functions with the maybe workflow. Is there a way for 2 workflows to share the same context? If no, what is the way of doing it? UPDATE: Well, I have a state that represents a segment of available ID's for the entities that I am going to create in the database. So once an ID is acquired the state has to be transformed to a newer state with the next available ID and thrown away so that nobody can use it again. I don't want to mutate the state for the sake of being idiomatic. The State monad looks like a way to go as it hides the

Get value from IO rather than the computation itself

巧了我就是萌 提交于 2019-12-02 06:29:56
问题 Being quite new to Haskell, I'm currently trying to improve my skills by writing an interpreter for a simple imperative toy language. One of the expressions in this language is input , which reads a single integer from standard input. However, when I assign the value of this expression to a variable and then use this variable later, it seems ot me that I actually stored the computation of reading a value rather the read value itself. This means that e.g. the statements x = input; y = x + x;

How does the List monad work in this example?

耗尽温柔 提交于 2019-12-02 05:43:39
问题 The List monad has return x = [x] . So why in the following example is the result not [(["a", "b"], [2, 3])] ? > pairs a b = do { x <- a; y <- b; return (x, y)} > pairs ["a", "b"] [2,3] [("a",2),("a",3),("b",2),("b",3)] 回答1: Let us first analyze and rewrite the function pairs : pairs a b = do { x <- a; y <- b; return (x, y)} Here we thus have a monad. We use do as syntactical sugar. But the compiler rewrites this to: pairs a b = a >>= (\x -> b >>= (\y -> return (x, y))) Or in a more canonical

Is do notation used necessarily in the context of monad?

[亡魂溺海] 提交于 2019-12-02 04:21:38
Haskell report 2010 says A do expression provides a more conventional syntax for monadic programming . It allows an expression such as putStr "x: " >> getLine >>= \l -> return (words l) to be written in a more traditional way as: do putStr "x: " l <- getLine return (words l) Haskell the Craft of Functional programming by Thompson says We'll continue to use the do notation, but will keep in mind that it essentially boils down to the existence of a function (>>=) which does the work of sequencing I/O programs, and binding their results for future use. Do the above mean that do notation is used

Dispatching to correct function with command line arguments in Haskell

淺唱寂寞╮ 提交于 2019-12-02 03:40:07
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) ("EDecipher", ExptCiphers.exptDecipherString) ("CEncipher", CaesarCiphers.caesarEncipherString) ("CDecipher"

How does the List monad work in this example?

冷暖自知 提交于 2019-12-02 00:54:51
The List monad has return x = [x] . So why in the following example is the result not [(["a", "b"], [2, 3])] ? > pairs a b = do { x <- a; y <- b; return (x, y)} > pairs ["a", "b"] [2,3] [("a",2),("a",3),("b",2),("b",3)] Let us first analyze and rewrite the function pairs : pairs a b = do { x <- a; y <- b; return (x, y)} Here we thus have a monad. We use do as syntactical sugar. But the compiler rewrites this to: pairs a b = a >>= (\x -> b >>= (\y -> return (x, y))) Or in a more canonical form: pairs a b = (>>=) a (\x -> (>>=) b (\y -> return (x, y))) Now the list monad is defined as: instance