posts/ccgoto: add scheme tag
This commit is contained in:
parent
e5d9e0254f
commit
2748bc5640
2 changed files with 59 additions and 1 deletions
58
site-src/burrito.mdx
Normal file
58
site-src/burrito.mdx
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
---
|
||||
title: "Monads: Why and how"
|
||||
description: I could not repress the urge to make a monad explainer any longer. Did you know that a monad is a monoid in the category of endofunctors?
|
||||
tags: post,short,haskell
|
||||
date: 2026-05-06 01:20:10 -5
|
||||
---
|
||||
|
||||
I acknowledge that [the world wide web has one hundred trillion monad explainers.](https://wiki.haskell.org/Monad_tutorials_timeline)
|
||||
|
||||

|
||||
|
||||
## Why monads
|
||||
In the land of Haskell and friends, we love referential transparency.
|
||||
We greatly appreciate that
|
||||
```haskell
|
||||
let x = 1 in x + x
|
||||
```
|
||||
will always have the same result as
|
||||
```haskell
|
||||
1 + 1
|
||||
```
|
||||
|
||||
In languages where (almost) no functions have unmarked side-effects, like Haskell, you can even be confident that
|
||||
```haskell
|
||||
let x = f y in x + x
|
||||
```
|
||||
is the same as
|
||||
```haskell
|
||||
(f y) + (f y)
|
||||
```
|
||||
|
||||
This is a truly excellent property to have, but it causes some problems when your program starts interacting with the world.
|
||||
|
||||
Aside from referential transparency, we don't want to fix evaluation order if we don't have to;
|
||||
giving the compiler more freedom to reorder evaluation lets it perform more optimizations.
|
||||
And we always want more optimizations.
|
||||
|
||||
But, quite often, we *need* things to happen in a specific order.
|
||||
|
||||
Monads let us do two things:
|
||||
1. Order operations
|
||||
2. Use past results to decide what to do next
|
||||
|
||||
## How monads
|
||||
Easy:
|
||||
```haskell
|
||||
class Monad m where
|
||||
(>>=) :: m a -> (a -> m b) -> m b
|
||||
pure :: a -> m a
|
||||
```
|
||||
|
||||
`>>=`, pronounced `bind`, gives us ordering and lets us use past results to decide what to do next.
|
||||
|
||||
The fact that it gives us ordering is made more clear if we define `>>`, which ignores its result:
|
||||
```haskell
|
||||
(>>) :: m a -> m b -> m b
|
||||
a >> b = a >>= \_ -> b
|
||||
```
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
---
|
||||
title: Emulating GOTO in Scheme with continuations
|
||||
description: GOTO sucks and is evil and I hate it, but what if there were parentheses? `call/cc` is kinda like goto, so let’s use it to make goto.
|
||||
tags: post,medium
|
||||
tags: post,medium,scheme
|
||||
date: 2026-02-18 14:28:46 -5
|
||||
---
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue