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
/* | |
* The inhabitants of Mars have 1, 2 or 3 arms. | |
* Once 5 Martians gathered and counted their arms: they had 10 arms in total. | |
* How many one-armed, two-armed and three-armed Martians gathered? | |
* Find all the options. | |
*/ | |
// (15, 5) -> [ 0, 0, 1, 2, 0 ] | |
const intToTernary = (i, size) => [...i.toString(3).padStart(size, "0")].map(Number); |
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
/* | |
* There are 6 dwarfs sitting at a round table. | |
* Each of them said: "Both of my neighbors are liars." | |
* What is the smallest number of liars that can sit at this table? | |
* What is the largest number of liars that can sit at this table? | |
*/ | |
// (12, 6) -> [0, 0, 1, 1, 0, 0] | |
const intToBits = (i, size) => [...i.toString(2).padStart(size, "0")].map(Number); |
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
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 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
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 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
function flatten(arr) { | |
return [].concat(...arr); | |
} | |
function combine(keys, values) { | |
return keys.reduce((acc, key, i) => | |
({ ...acc, [key]: values[i] || null }), {} | |
); | |
} |
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
-- 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 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
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 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
-- 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 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
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 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
-- 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) |
NewerOlder