How are functors in Haskell and OCaml similar?

I've been toying around in a Haskell for the past year or so and I'm actually starting to 'get' it, up until Monads, Lenses, Type Families, ... the lot. I'm about to leave this comfort zone a little and I am moving to an OCaml project as a day job. Going through the syntax a little I was looking for similar higher level concepts, like for example functor. I read the code

Haskell和OCaml中的函子如何相似?

在过去的一年里,我一直在Haskell玩耍,我实际上已经开始“得到”它,直到Monads,Lenses,Type Families,......很多。 我即将离开这个舒适区,我正在转向一个OCaml项目作为日常工作。 仔细查看语法,我正在寻找类似的更高层次的概念,例如functor。 我阅读了OCaml中的代码和一个函数的结构,但我似乎无法弄清楚他们现在是否在Haskell和OCaml中有类似的概念。 简而言之,Haskell中的函数对我来说主要是一种在Haskell中提升

Can functional reactive programming (FRP) be expressed using monads?

I've been reading up on Functional Reactive Programming, and though I have not used monads extensively in any language, I can't help but see them everywhere in the FRP design. This question's answers have some fantastic descriptions of what functional reactive programming is, and I won't attempt to replicate that here. Basically, FRP creates relationships between values that ch

功能反应式编程(FRP)可以用monads来表示吗?

我一直在阅读Functional Reactive Programming,虽然我没有广泛地使用monad,但我不禁要在FRP设计中的任何地方看到它们。 这个问题的答案对功能性反应式编程有一些奇妙的描述,而且我不会试图在这里复制它。 基本上,FRP会在随时间变化的值之间建立关系。 那么这不能一次表示吗? 将需要随时间修改的值封装在monad中,将其称为Signal ,然后像这样使用这些信号(为简单起见,使用Haskell do-notation)。 do mx <- mo

Why does FRP consider time as a factor for values?

Behaviors are ubiquitously defined as “time-varying value”s1. Why? time being the dependency/parameter for varying values is very uncommon. My intuition for FRP would be to have behaviors as event-varying values instead; it is much more common, much more simple, I wage a much more of an efficient idea, and extensible enough to support time too (tick event). For instance, if you write a co

为什么FRP将时间视为价值的一个因素?

行为无处不在被定义为“时变价值”s1。 为什么? 时间是变化值的依赖/参数是非常罕见的。 我对FRP的直觉是将行为看作事件变化的价值观; 它更常见,更简单,我提出了更有效的想法,并且可扩展性足以支持时间(tick事件)。 例如,如果你写一个计数器,你不关心时间/相关的时间戳,你只关心“增加按钮点击”和“减少按钮点击”事件。 如果你写一个游戏并想要一个位置/强制行为,你只关心WASD /箭头键举行的事件等(除非你禁止

Free monad and the free operation

One way to describe the Free monad is to say it is an initial monoid in the category of endofunctors (of some category C ) whose objects are the endofunctors from C to C , arrows are the natural transformations between them. If we take C to be Hask , the endofunctor are what is called Functor in haskell, which are functor from * -> * where * represents the objects of Hask By initiality, any

免费的monad和免费的操作

描述自由单体的一种方式是说它是内功能体(属于某种类别C )类别中的一个初始幺半群,其对象是从C到C的末端执行体,箭头是它们之间的自然变换。 如果我们把C作为Hask ,endofunctor就是Haskell中所谓的Functor ,它是来自* -> *函子,其中*表示Hask的对象 按照初始性,从endofunctor t到End(Hask)的monoid m任何映射End(Hask)引发一个从Free t到m的映射。 否则,从Functor t到Monad m任何自然转换都会诱导从Free t到m的

Why is "bind" written as >>= in Haskell Monads?

What are the origins of the syntax ">>=" and ">>" in Haskell Monads? I'm not looking for a explanation of how Monads work but instead why the language designers chose that syntax. To me, ">>=" and ">>" seems kind of arbitrary and never made intuitive sense. Does anyone have a intuitive explanation? Is it syntax that comes fro

为什么在Haskell Monads中将“bind”写为>> =?

Haskell Monads中语法“>> =”和“>>”的起源是什么? 我不是在寻找Monad如何工作的解释,而是为什么语言设计者选择了这种语法。 对我而言,“>> =”和“>>”似乎有些随意,从来没有直观的意义。 有没有人有一个直观的解释? 它是来自类别理论的语法吗? m >>= k表示“将计算结果m送到函数k ”; m >> n建议“运行m计算,然后运行n计算”。

Applicative instance for free monad

While trying to find a haskell monad that can be executed stepwise / allows threading, I discovered the free monad data Free f a = Return a | Roll (f (Free f a)) with its monad instance instance (Functor f) => Monad (Free f) where return = Return Return x >>= f = f x Roll action >>= f = Roll $ fmap (>>= f) action and its functor instance instance (Functor f) =&g

适用于免费monad的应用程序实例

当试图找到一个可以逐步执行/允许线程的哈斯克尔monad时,我发现了免费的monad data Free f a = Return a | Roll (f (Free f a)) 与它的monad实例 instance (Functor f) => Monad (Free f) where return = Return Return x >>= f = f x Roll action >>= f = Roll $ fmap (>>= f) action 和它的函子实例 instance (Functor f) => Functor (Free f) where fmap f (Return x) = Return (f x

Applying Semantics to Free Monads

I am trying to abstract the pattern of applying a certain semantics to a free monad over some functor. The running example I am using to motivate this is applying updates to an entity in a game. So I import a few libraries and define a few example types and an entity class for the purposes of this example (I am using the free monad implementation in control-monad-free): {-# LANGUAGE DeriveFunc

将语义应用于免费Monad

我试图通过一些函子抽象出将某种语义应用于自由monad的模式。 我用来激励这个的运行示例是将更新应用于游戏中的实体。 所以我导入了一些库,并为本例的目的定义了一些示例类型和一个实体类(我使用免费monad实现在无monad控件中): {-# LANGUAGE DeriveFunctor #-} {-# LANGUAGE TypeFamilies #-} import Control.Monad.Free import Control.Monad.Identity import Control.Monad.Writer -- Things which can happen to an

>) instances of Monad and confusion about (

At different questions I've found hints in comments concerning using the (->) instance of Monads eg for realizing point-free style. As for me, this is a little too abstract. Ok, I've seen Arrow instances on (->) and it seems to me, that (->) can be used in instance notations but not in type declarations (that would alone be stuff for another question). Has anyone examples us

>)Monad的实例和混淆(

在不同的问题中,我发现了有关使用Monads (->)实例的注释,例如实现无点风格。 至于我,这太抽象了。 好的,我在(->)上看到了Arrow实例,在我看来, (->)可以用于实例符号中,但不能用于类型声明中(这仅仅是另一个问题的东西)。 有没有人使用(->)作为Monad的实例? 或者一个好的链接? 很抱歉,如果这个问题可能已经在这里讨论过了,但是搜索“ (->) Monad实例”会给你很多点击,就像你想象的那样...因为

Monads as adjunctions

I've been reading about monads in category theory. One definition of monads uses a pair of adjoint functors. A monad is defined by a round-trip using those functors. Apparently adjunctions are very important in category theory, but I haven't seen any explanation of Haskell monads in terms of adjoint functors. Has anyone given it a thought? Edit : Just for fun, I'm going to do th

Monads作为附件

我一直在阅读有关monad的分类理论。 单子的一个定义使用一对伴随函数。 monad通过使用这些函子的往返来定义。 显然助理在类别理论中非常重要,但我从来没有在伴随函数方面看到任何Haskell单子的解释。 有没有人给过它一个想法? 编辑 :只是为了好玩,我会正确地做到这一点。 原始答案保存在下面 现在,类别附加组件的当前附加代码位于附件包中:http://hackage.haskell.org/package/adjunctions 我只是明确而简单地

Lazy evaluation for list generating functions?

I'm currently reading Programming in Haskell, by Graham Hutton. In p.40, a toy primality test is presented: factors :: Int -> [Int] factors n = [x | x <- [1..n], n `mod` x == 0] prime :: Int -> Bool prime n = factors n == [1,n] The author then goes on to explain how "deciding that a number is not prime does not require the function prime to produce all of its factors, bec

列表生成函数的懒惰评估?

我目前正在阅读Graham Hutton编写的Haskell编程。 在第40页中,提出了玩具素性测试: factors :: Int -> [Int] factors n = [x | x <- [1..n], n `mod` x == 0] prime :: Int -> Bool prime n = factors n == [1,n] 作者接着解释如何 “决定一个数不是素数并不要求函数素数产生所有的因素,因为在懒惰的评估中,一旦产生除了一个或数字本身以外的任何因素, False就返回” 作为来自C和Java的人,我觉得这很令人