Skip to content

Commit 639601c

Browse files
committed
fix comonads
1 parent 86f7b55 commit 639601c

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/content/blog/aoc2024/index.md

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,27 @@ day4part2 input = do
223223

224224
By this time, I found some other posts about people using Haskell for AoC, and
225225
found about the `Comonad` class, implemented by `Store`. They used the
226-
properties of `Comonad`, which are the following:
226+
properties of `Comonad`, which are the following (simplified).
227227

228228
```haskell
229+
class Monad m where
230+
return :: a -> m a
231+
(>>=) :: m a -> (a -> m b) -> m b
229232

233+
class Comonad w where
234+
extract :: w a -> a
235+
extend :: (w a -> b) -> w a -> w b
230236
```
231237

232-
Having a `Store` of points allows you to also define a function from Store of
233-
points to check if there is a cross, and the extend this function to get a Store
234-
of checks. Neat!
238+
Similarly to `Monad`, but reverse, `extend` takes a function of a wrapped value
239+
to a value (the opposite of bind), and `extract` is the opposite operation of
240+
`return`.
241+
242+
Having a `Store` that implements `Comonads`, allows you to:
243+
244+
- Extract the central value of the Store (which can be sought).
245+
- Apply a function that takes a `Store` of points to check if it is a cross, and
246+
then extend it to all possible values.
235247

236248
## Day 5: More parsing
237249

0 commit comments

Comments
 (0)