Monads: Difference between revisions

From bibbleWiki
Jump to navigation Jump to search
Created page with "=Introduction= Heard about this with a friend and never really revisited it. With my attention at home on Rust, thought I might write something down =The Maybe Monad= Well possibly the Option monad for me. To make one of these you need <syntaxhighlight lang="hs"> return :: a => Maybe a >>= : Maybe a </syntaxhighlight> =What's the Point= *Same idea works for other effects *Supports pure programming with effects *Use of effects explicit in types *Functions that work for..."
 
 
(2 intermediate revisions by the same user not shown)
Line 2: Line 2:
Heard about this with a friend and never really revisited it. With my attention at home on Rust, thought I might write something down
Heard about this with a friend and never really revisited it. With my attention at home on Rust, thought I might write something down
=The Maybe Monad=
=The Maybe Monad=
Well possibly the Option monad for me. To make one of these you need
Well possibly the Option monad for me. To make one of these you need define something to wrap your thing with and a function which could fail that returns the wrapper type
<syntaxhighlight lang="hs">
<syntaxhighlight lang="hs">
return :: a => Maybe a
return :: a => Maybe a
>>= : Maybe a
>>= :: Maybe a
</syntaxhighlight>
</syntaxhighlight>
 
This was explained to me a little better with this picture.<br>
[[File:Monads.png|700px]]<br>


=What's the Point=
=What's the Point=
*Same idea works for other effects
*Same idea works for other effects, e.g. reading from environments, input/output
*Supports pure programming with effects
*Supports pure programming with effects  
*Use of effects explicit in types
*Use of effects explicit in types
*Functions that work for any effect
*Functions that work for any effect

Latest revision as of 04:25, 10 February 2025

Introduction

Heard about this with a friend and never really revisited it. With my attention at home on Rust, thought I might write something down

The Maybe Monad

Well possibly the Option monad for me. To make one of these you need define something to wrap your thing with and a function which could fail that returns the wrapper type

return :: a => Maybe a
>>= :: Maybe a

This was explained to me a little better with this picture.

What's the Point

  • Same idea works for other effects, e.g. reading from environments, input/output
  • Supports pure programming with effects
  • Use of effects explicit in types
  • Functions that work for any effect