- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
In this discussion Brian Marick指出 let
和 for
是 Clojure 中的单子(monad):
That said, the really general-purpose monads tend to get written into the language as special forms. Clojure's
let
andfor
are both monads, but you don't need to know that to use them.
let
user=> (let [c (+ 1 2)
[d e] [5 6]]
(-> (+ d e) (- c)))
8
for
user=> (for [x [0 1 2 3 4 5]
:let [y (* x 3)]
:when (even? y)]
y)
(0 6 12)
let
和 for
两个单子(monad)?
最佳答案
为什么 Clojure 的 let
和 for
两个单子(monad)?
他们不是。
Clojure 的 let
和 for
不是单子(monad),因为它们没有完全暴露它们的单子(monad)共同结构。它们更像是糖 jail 里的单子(monad)。
什么是单子(monad)?
用 Clojure 的说法,一个 monad 可以被描述为一个 Monad 协议(protocol)的具体化,它的函数被期望以某些明确定义的方式相互运行并在具体化类型上运行。这并不是说 monad 必须用 defprotocol
来实现。 , reify
, 和 friend ,但这给出了这个想法,而不必谈论类型类或类别。
(defprotocol Monad
(bind [_ mv f])
(unit [_ v]))
(def id-monad
(reify Monad
(bind [_ mv f] (f mv))
(unit [_ v] v)))
(def seq-monad
(reify Monad
(bind [_ mv f] (mapcat f mv))
(unit [_ v] [v])))
(bind seq-monad (range 6) (fn [a]
(bind seq-monad (range a) (fn [b]
(unit seq-monad (* a b))))))
;=> (0 0 2 0 3 6 0 4 8 12 0 5 10 15 20)
(defn do-monad-comp
[monad body return]
(reduce
(fn [a [exp sym]] (list 'bind monad exp (list 'fn [sym] a)))
(list 'unit monad return)
(partition 2 (rseq body))))
(defmacro do-monad [monad body return]
(do-monad-comp monad body return))
(do-monad seq-monad
[a (range 6)
b (range a)]
(* a b))
;=> (0 0 2 0 3 6 0 4 8 12 0 5 10 15 20)
(for
[a (range 6)
b (range a)]
(* a b))
;=> (0 0 2 0 3 6 0 4 8 12 0 5 10 15 20)
(do-monad id-monad
[a 6
b (inc a)]
(* a b))
;=> 42
(let
[a 6
b (inc a)]
(* a b))
;=> 42
for
就像序列 monad 和
let
就像身份单子(monad),但在糖化表达的范围内。
bind
来定义。和
unit
, 例如
(defn fmap
[monad f mv]
(bind monad mv (fn [v] (unit monad (f v)))))
(fmap id-monad inc 1)
;=> 2
(fmap seq-monad inc [1 2 3 4])
;=> (2 3 4 5)
let
和
for
不要完全公开这个通用结构,因此不能完全参与(以通用方式)。
关于clojure - 为什么 Clojure 的 `let` 和 `for` 都是单子(monad)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21752258/
monad 被定义为类别 C 上的内仿函数。假设 C 具有类型 int 和 bool 以及其他构造类型作为对象。现在让我们考虑在这个类别上定义的列表 monad。 根据它的定义,list 是一个内仿函
我试图采取例如ExceptT a (StateT A M) , 对于某些具体类型 A和单子(monad)M ,并将它们包装到我的新自定义单子(monad)中。 首先我确定StateT A M经常出现在
我读到(例如 here 和 here )所有基本单子(monad)(Mabye, Error, ...) 源自其相应的 monad 转换器(MaybeT, ErrorT, ...) 使用身份 mona
Haskell 的状态单子(monad) State s a迫使我保持相同类型的 s在整个做 block 期间。但是由于 state monad 实际上只是一个函数,如果我将它定义为 State
我一直在阅读some materials on free monads而且我真的不认为我离实现更近了,但我认为我更接近于理解它们是什么! 鉴于上述大量资源,我的理解是自由单子(monad)从“计算”工
假设我有一个由两个 monad 操作组成的函数: co::Monad m => m a -> m a -> m a 您可以将 co 视为一个高阶函数,它描述两个单子(monad)操作如何相互协作来完成
在 SO解释了为什么像 scalaz、cats (Scala) 或 Arrow (Kotlin) 中的 Validation 不能是 monad。 据我所知,这是因为他们已经根据应用仿函数对 mona
我对 Haskell 还很陌生,并且慢慢地意识到 Monad fail 的存在有问题。真实世界的 Haskell warns against its use (“再一次,我们建议您几乎总是避免使用失败
我正在阅读现实世界 Haskell 中的 monad 转换器。在以下示例中,堆栈为 Writer在顶部State在Reader之上在IO之上。 {-# Language GeneralizedNewt
我看到的典型 Pause monad 实现如下所示(基于 Giulia Costantini 和 Giuseppe Maggiore 编写的 Friendly F# 的第 5 章)。 open Sys
“Monads 允许程序员使用顺序构建 block 来构建计算”,因此它允许我们组合一些计算。如果是这样,那为什么下面的代码不能运行呢? import Control.Monad.Trans.Stat
这是我第一次认识 Monad Transformers,所以答案可能很明显。 假设我在 StateT MyMonad MyType 类型的 do 块中,我想让另一个相同类型的函数修改状态并返回 MyM
人们通常说类型是单子(monad)。 在某些函数式语言和库(如 Scala/Scalaz)中,您有一个类型构造函数,如 List 或 Option,您可以定义一个与原始类型分离的 Monad 实现。所
我的目标是创建一个函数,该函数在 ReaderT WriterT 堆栈或 RWS 堆栈中使用 list monad。更一般地说,我如何在 mtl 类型类(如 MonadReader、MonadWrit
我只是想知道是否有一个简洁的术语来表示既是单子(monad)又是单子(monad)的东西。我做了一些搜索,我知道these structures exist ,但我还没有找到他们的名字。 最佳答案 在
我正在玩写一个网络应用程序。在这种情况下,我使用 scotty和 redis ,但是这个问题出现在任何 web/db 组合中。在此之前我使用了 happstack,所以我也喜欢那里的一个例子。 Sco
是 x >>= f相当于 retract (liftF x >>= liftF . f) ? 也就是说,从同样是 Monad 的 Functor 构建的自由 monad 的 monad 实例是否将具有
我正在尝试编写一个只能包含 Num 的新 monad。当它失败时,它返回 0,就像 Maybe monad 在失败时返回 Nothing 一样。 这是我到目前为止所拥有的: data (Num a)
我正在使用 operational monad作者:海因里希·阿普菲尔姆斯。 我想用结果类型的 monad 参数化解释器。 我的代码的以下版本编译: {-# LANGUAGE GADTs #-} im
假设所有的 monad 都可以用 Free 来表示。 (如果这不是真的,什么是反例,为什么)?怎么可能the continuation monad或其对应的变压器用 Free 表示或 FreeT -
我是一名优秀的程序员,十分优秀!