From a97447840ee84cc509343c93564afceddf98ee3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Fri, 19 Nov 2021 19:41:24 +0100 Subject: [PATCH] Update --- monad.md | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 monad.md diff --git a/monad.md b/monad.md new file mode 100644 index 0000000..e8714b5 --- /dev/null +++ b/monad.md @@ -0,0 +1,18 @@ +# Monad + +{ This is my poor understanding of a monad. ~drummyfish } + +Monad is a [mathematical](math.md) concept which has become useful in [functional programming](functional.md) and is one of the very basic [design patterns](design_pattern.md) in this paradigm. A monad basically wraps some [data type](data_type) into an "envelope" type and gives a way to operate with these wrapped data types which greatly simplifies things like error checking or abstracting [input/output](io.md) side effects. + +A typical example is a **maybe** monad which wraps a type such as integer to handle exceptions such as division by zero. A maybe monad consists of: + +1. The *maybe(T)* data type where *T* is some other data type, e.g. *maybe(int)*. Type *maybe(T)* can have these values: + - *just(X)* where *X* is any possible value of *T* (for int: -1, 0, 1, 2, ...), or + - *nothing*, a special value that says no value is present +2. A special function *return(X)* that converts value of given type into this maybe type, e.g. *return(3)* will return *just(3)* +3. A special combinator *X >>= f* which takes a monadic (*maybe*) values *X* and a function *f* and does the following: + - if *X* is *nothing*, gives back *nothing* + - if *X* is a value *just(N)*, gives back the value *f(N)* + + +TODO: example in some lang, e.g. haskell