编程与软件工程adit.io编辑精选
图解函子、应用函子与单子
Functors, applicatives, and monads in pictures
2013 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这是 Aditya Bhargava 2013 年的经典图解教程,用直观的图片逐步解释 Haskell 中函子、应用函子和单子的概念,例如 fmap 如何把普通函数作用于包装在上下文(如 Maybe)中的值。它多年来反复登上 HN 首页,被视为最容易上手的单子入门材料之一。
为什么经久不衰:它把抽象的范畴论概念变成一张张直观的图,降低了函数式编程的入门门槛,至今仍是初学者理解单子的首选读物。
HN 评论精选
单子教程的通病是缺乏动机:应先讲清 Haskell 中副作用受限的背景,而不是把单子当成纯度问题的补丁。
很多单子教程(包括这篇)都掉进同一个陷阱:不讲动机,或者讲得好像单子只是为了解决纯函数语言带来的问题。在普通语言里你随时可以做副作用;而 Haskell 默认没有任何副作用,想用副作用时才用单子,用哪种单子决定你能有哪些效果,比如 Maybe 单子给你“无错误地失败”,Either 给你“带错误地失败”,列表单子给你非确定性。
DanWaterworth查看英文原文
I think that many monad tutorials, including this one, fall into the same trap. They explain monads without giving any motivation, or when they do, it seems like monads only exist to get around problems caused by being pure. When you program in normal languages, you have the ability to perform effects all of the time. You can always read/write to/from files, you can always raise exceptions or otherwise change the control flow. There's nothing you can't do. Haskell is different. In Haskell, by default, you don't have any effects at your disposal at all. You can create values from other values, that's it. When you want to program with effects, you use a monad. The type of monad that you use decides the effects that you are allowed. For example, the maybe monad instance gives you the "fail without an error effect". The either monad gives you the "fail with an error effect". The list monad instance gives you the non-determinism effect. etc. You might expect me to say that IO gives you the "interact with the outside world effect", but that's not really true, forget what you know about IO for the moment. The continuation monad instance is a bit special. In a sense, it is the most general of the monad types, because every monad instance can be seen as a specialization of it. The continuation monad allows every kind of effect. At this point, you might be thinking, "but how can this be, it doesn't allow IO". To reconcile these facts. I present the coroutine monad. It has an effect, yield, that gives a value to the caller and waits for the caller to respond with a value so that it can continue. Here are some types: runCoroutine :: Coroutine i o x -> Either (o, i -> Coroutine i o x) x yield :: o -> Coroutine i o i So running a coroutine either produces a value of o and function to resume the coroutine or it completes with a value of x. IO is very similar to the coroutine monad. When a Haskell program gets to a function that talks to the outside world it yields control to the runtime environment, the runtime environment does the actual IO and passes the result back so that the program can continue. This is why Haskell is pure despite having effects. Haskell programs don't do IO, they yield control to the runtime environment whenever IO needs to be done. So why are monads useful? They allow you to restrict the effects that are possible in different parts of your programs. This aids in reasoning about your programs, because you don't have to think about whole classes of behaviour, but they're less useful in other languages where you already have effects everywhere.
图解容易误导人以为 bind 只调用一次函数,实际上调用次数可零可多,这是理解单子的关键。
这种单子可视化有一个常见隐患:它强烈暗示输入和输出是一一对应的,等于说 bind 只会调用一次传入的函数。实际上可能调用零次(Nothing、空列表)、一次(Just、单元素列表)或多次(列表单子对每个元素各调一次),这是理解单子接口的关键。
jerf查看英文原文
This has one of the usual dangers of trying to do a visualization of a monad, which is that it strongly implies that there is a one-to-one relationship between what you put in and what comes out, which is basically saying that the bind function of the monadic interface implementation will only call the function it receives once. This is not true; it may be called zero times (Nothing, empty list), once (Just something, a list of one element), or many times (once for each element in the list for the list monad, for instance), which is a critical element to understanding the monad interface. So, you know, be aware of that I guess.
对不懂 Haskell 的读者,文章在几页之内就变得难以跟上。
不情愿地承认:完全不懂 Haskell 的我,到第四张图就跟不上了。如果你懂 Haskell,“data Maybe a = Nothing | Just a”大概能看懂,但我完全解析不了。
seldo查看英文原文
I reluctantly confess that, knowing nothing about Haskell, I was lost by the 4th diagram. Presumably if you know Haskell, the line "data Maybe a = Nothing | Just a" makes sense, but I can't parse it.
这是最平易近人的单子指南之一,是推荐给初学者的首选。
满满的回忆——这是我看过的最好的单子入门指南之一。和《Learn You A Haskell For Great Good》类似,虽然不是最深入或最完整的,但往往是我第一个推荐给别人看的材料。
hardwaresofton查看英文原文
This absolutely takes me back -- this is one of the best easily-approachable guides to Monads I've ever seen. Similar to Learn You A Haskell For Great Good (colloquially "LYAH"), even if it's not the most in-depth or complete, it's the first thing I often send
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选