monads

How to implement a stack-safe chainRec operator for the continuation monad?

耗尽温柔 提交于 2019-12-17 17:05:46
问题 I am currently experimenting with the continuation monad. Cont is actually useful in Javascript, because it abstracts from the callback pattern. When we deal with monadic recursion, there is always the risk of a stack overflow, because the recursive call isn't in tail position: const chain = g => f => k => g(x => f(x) (k)); const of = x => k => k(x); const id = x => x; const inc = x => x + 1; const repeat = n => f => x => n === 0 ? of(x) : chain(of(f(x))) (repeat(n - 1) (f)); console.log(

Defining a new monad in haskell raises no instance for Applicative

為{幸葍}努か 提交于 2019-12-17 15:38:14
问题 I am trying to define a new monad and I am getting a strange error newmonad.hs newtype Wrapped a = Wrap {unwrap :: a} instance Monad Wrapped where (>>=) (Wrap x) f = f x return x = Wrap x main = do putStrLn "yay" $ ghc --version The Glorious Glasgow Haskell Compilation System, version 7.10.1 $ ghc newmonad.hs [1 of 1] Compiling Main ( newmonad.hs, newmonad.o ) newmonad.hs:2:10: No instance for (Applicative Wrapped) arising from the superclasses of an instance declaration In the instance

Is this property of a functor stronger than a monad?

一个人想着一个人 提交于 2019-12-17 15:34:33
问题 While thinking about how to generalize monads, I came up with the following property of a functor F: inject :: (a -> F b) -> F(a -> b) -- which should be a natural transformation in both a and b. In absence of a better name, I call the functor F bindable if there exists a natural transformation inject shown above. The main question is, whether this property is already known and has a name, and how is it related to other well-known properties of functors (such as, being applicative, monadic,

Why should Applicative be a superclass of Monad?

被刻印的时光 ゝ 提交于 2019-12-17 15:24:32
问题 Given: Applicative m, Monad m => mf :: m (a -> b), ma :: m a it seems to be considered a law that: mf <*> ma === do { f <- mf; a <- ma; return (f a) } or more concisely: (<*>) === ap The documentation for Control.Applicative says that <*> is "sequential application," and that suggests that (<*>) = ap . This means that <*> must evaluate effects sequentially from left to right, for consistency with >>= ... But that feels wrong. McBride and Paterson's original paper seems to imply that the left

Is Haskell truly pure (is any language that deals with input and output outside the system)?

女生的网名这么多〃 提交于 2019-12-17 07:12:57
问题 After touching on Monads in respect to functional programming, does the feature actually make a language pure, or is it just another "get out of jail free card" for reasoning of computer systems in the real world, outside of blackboard maths? EDIT: This is not flame bait as someone has said in this post, but a genuine question that I am hoping that someone can shoot me down with and say, proof, it is pure. Also I am looking at the question with respect to other not so pure Functional

Large-scale design in Haskell? [closed]

£可爱£侵袭症+ 提交于 2019-12-17 04:36:13
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . Locked . This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions. What is a good way to design/structure large functional programs, especially in Haskell? I've been

Confused with the for-comprehension to flatMap/Map transformation

╄→尐↘猪︶ㄣ 提交于 2019-12-17 03:22:05
问题 I really don't seem to be understanding Map and FlatMap. What I am failing to understand is how a for-comprehension is a sequence of nested calls to map and flatMap. The following example is from Functional Programming in Scala def bothMatch(pat:String,pat2:String,s:String):Option[Boolean] = for { f <- mkMatcher(pat) g <- mkMatcher(pat2) } yield f(s) && g(s) translates to def bothMatch(pat:String,pat2:String,s:String):Option[Boolean] = mkMatcher(pat) flatMap (f => mkMatcher(pat2) map (g => f

What is the point of the class Option[T]?

拥有回忆 提交于 2019-12-17 02:58:29
问题 I am not able to understand the point of Option[T] class in Scala. I mean, I am not able to see any advanages of None over null . For example, consider the code: object Main{ class Person(name: String, var age: int){ def display = println(name+" "+age) } def getPerson1: Person = { // returns a Person instance or null } def getPerson2: Option[Person] = { // returns either Some[Person] or None } def main(argv: Array[String]): Unit = { val p = getPerson1 if (p!=null) p.display getPerson2 match{

non-monadic error handling in Haskell?

杀马特。学长 韩版系。学妹 提交于 2019-12-14 03:51:34
问题 I was wondering if there is an elegant way to do non-monadic error handling in Haskell that is syntactically simpler than using plain Maybe or Either . What I wanted to deal with is non-IO exceptions such as in parsing, where you generate the exception yourself to let yourself know at a later point, e.g., something was wrong in the input string. The reason I ask is that monads seem to be viral to me. If I wanted to use exception or exception-like mechanism to report non-critical error in pure

Chaining method calls with Either

﹥>﹥吖頭↗ 提交于 2019-12-14 03:42:05
问题 I'd like to know if it is possible to create some kind of "method call chain", with all methods returning the same Either[Error,Result]. What i'd like to do is: call all the methods successively, and when method returns a Left(Error), then stop the method calls and return the first Left found in the call chain. I've tryied some stuff, with fold, map, projections... but i'm new to Scala and don't find any elegant solution. I've tryed some thing like that: def createUserAndMandatoryCategories