Last active
August 29, 2015 14:04
-
-
Save Heimdell/c71de51a522ba28aa006 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- three next declarations are declaring the same thing -} | |
test0 :: [Int] | |
test0 = do | |
x <- [2..5] -- for all x from [2.. 5] | |
y <- [x - 1, x, x + 1] -- for all y from [x - 1.. x + 1] | |
return y -- return y | |
{- | |
generates: | |
[1,2,3,2,3,4,3,4,5,4,5,6] | |
= [1,2,3, 2,3,4, 3,4,5, 4,5,6] -- forall x from xs, the ys returned and glued together | |
2 3 4 5 -- all xs | |
-} | |
test0_afterPrepocessor = | |
[2..5] >>= \x -> | |
[x - 1, x, x + 1] >>= \y -> | |
return y | |
test0_whereYouCanSeeAllLambdas = | |
[2..5] >>= | |
\x -> [x - 1, x, x + 1] >>= -- we are in the 1st lambda here | |
\y -> return y -- we are in the 1st and the 2nd lambdas here | |
{- | |
So, what are "list >>= function" operator and the "return"? | |
-} | |
{- | |
This is the replica of Monad typeclass (inteface!) from std lib | |
(I renamed "return" and ">>=", because compiler don't like redefining things. | |
If there is an "instance Monad SomeType", that SomeType could be used with | |
this methods. | |
-} | |
class Monad1 box where | |
{- take a value and put it into the box -} | |
return1 :: a -> box a | |
{- | |
take a "box with a", function from "a" to "box with b" | |
and make a "box with b" | |
so, it is the погрузочная воронка, блядь, не помню как это по английски | |
also, we may say, it takes a boxed value and a rest of the program, which can do something | |
with contents of the box, generating a box in the end | |
-} | |
(>>=.) :: box a -> (a -> box b) -> box b | |
{- | |
This is the replica of "instance Monad List" (implementation!) from stdlib | |
-} | |
instance Monad1 [] where | |
{- boxing into list -} | |
return1 a = [a] | |
{- | |
take a list and a function, apply it to the list (generating a list of lists) | |
then concat result to the plain list | |
so, the list monad is "calculate all possible outcomes" | |
-} | |
list >>=. f = | |
concat (map f list) | |
{- | |
This is the meaning of monad for lists; the other types will have (completely!) different ones. | |
-} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment