This file contains 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
data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show | |
bt = Branch (Leaf 42) (Branch (Leaf 1) (Leaf 2)) | |
fmap' :: (a -> b) -> Tree a -> Tree b | |
fmap' f (Leaf x) = Leaf (f x) | |
fmap' f (Branch l r) = Branch (fmap' f l) (fmap' f r) | |
foldMap' :: Monoid m => (a -> m) -> Tree a -> m | |
foldMap' f (Leaf x) = f x |
This file contains 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
data RoseTree a = Rose a [RoseTree a] | |
deriving Show | |
-- 5 | |
-- / \ | |
-- 3 7 | |
-- / \ | |
-- 1 4 | |
rt = Rose 5 [Rose 3 [Rose 1 [], Rose 4[]], Rose 7 []] |
This file contains 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
function flatten(arr) { | |
return [].concat(...arr); | |
} | |
function combine(keys, values) { | |
return keys.reduce((acc, key, i) => | |
({ ...acc, [key]: values[i] || null }), {} | |
); | |
} |
This file contains 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
-- https://web.cecs.pdx.edu/~mpj/pubs/springschool95.pdf | |
-- https://patternsinfp.wordpress.com/2015/03/05/breadth-first-traversal/ | |
-- https://researchspace.auckland.ac.nz/handle/2292/3470 | |
class Tree t where | |
subtrees :: t -> [t] | |
data BinTree a = Leaf a | |
| BinTree a :^: BinTree a |
This file contains 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
import Control.Monad | |
import Control.Monad.Trans | |
import Control.Monad.Trans.Maybe | |
isValid :: String -> Bool | |
isValid [] = False | |
isValid (x:_) = x == 'a' | |
getPsw :: MaybeT IO String | |
getPsw = do s <- lift $ getLine |
This file contains 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
-- Prelude Control.Monad> :t (>=>) | |
-- (>=>) :: Monad m => (a -> m b) -> (b -> m c) -> a -> m c | |
-- Prelude Control.Monad> :t (id >=>) | |
-- (id >=>) :: Monad m => (b -> m c) -> m b -> m c | |
-- Prelude Control.Monad> :t \f -> (>=> \x -> return (f x)) | |
-- \f -> (>=> \x -> return (f x)) | |
-- :: Monad m => (t -> c) -> (a -> m t) -> a -> m c |
This file contains 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
class Profunctor p where | |
dimap :: (a -> b) -> (c -> d) -> p b c -> p a d | |
dimap f g = lmap f . rmap g | |
lmap :: (a -> b) -> p b c -> p a c | |
lmap f = dimap f id | |
rmap :: (b -> c) -> p a b -> p a c | |
rmap = dimap id | |
instance Profunctor (->) where | |
-- dimap :: (a -> b) -> (c -> d) -> (b -> c) -> (a -> d) |
This file contains 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
-- https://en.wikibooks.org/wiki/Haskell/Continuation_passing_style | |
add :: Int -> Int -> Int | |
add x y = x + y | |
square :: Int -> Int | |
square x = x * x | |
pithagoras :: Int -> Int -> Int | |
pithagoras x y = add (square x) (square y) |
This file contains 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
-- exercices for | |
-- https://en.wikibooks.org/wiki/Haskell/Understanding_monads/State | |
import Control.Monad | |
import Control.Monad.Trans.State | |
import System.Random | |
rollDie :: State StdGen Int | |
rollDie = state $ randomR (1,6) |
This file contains 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
--- functor from applicative --- | |
-- fmap2 (2*) [1,2,3,4] | |
fmap2 :: Applicative f => (a -> b) -> f a -> f b | |
-- fmap2 f x = pure f <*> x | |
-- fmap2 f = (<*>) (pure f) | |
fmap2 = (<*>) . pure | |
--- functor from monad (liftM) | |
-- fmap3 (2*) [1,2,3,4] | |
fmap3 :: Monad m => (a -> b) -> m a -> m b |
NewerOlder