Last active
November 7, 2018 16:50
-
-
Save AndrasKovacs/1216d7e7137d275a90f0fd8807112e21 to your computer and use it in GitHub Desktop.
Funlist permutations challenge
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
{-# language GADTs #-} | |
-- https://www.reddit.com/r/haskell/comments/9uz2f5/code_challenge_welltyped_tree_node_order/ | |
data Tree a where | |
OneT :: a -> Tree a | |
Ap :: (a -> b -> c) -> Tree a -> Tree b -> Tree c | |
data FunList a where | |
OneL :: a -> FunList a | |
Cons :: (a -> b -> c) -> a -> FunList b -> FunList c | |
insertions :: (a -> b -> c) -> a -> FunList b -> [FunList c] | |
insertions f x (OneL y) = [Cons f x (OneL y), Cons (flip f) y (OneL x)] | |
insertions f x (Cons g y ys) = | |
Cons f x (Cons g y ys) | |
: map (Cons (\a e -> f x (g a e)) y) (insertions (\_ e -> e) x ys) | |
permL :: FunList a -> [FunList a] | |
permL (OneL x) = [OneL x] | |
permL (Cons f x xs) = concatMap (insertions f x) (permL xs) | |
apL :: (a -> b -> c) -> FunList a -> FunList b -> FunList c | |
apL f (OneL x) ys = Cons f x ys | |
apL f (Cons g x xs) ys = Cons (\a (b, c) -> f (g a b) c) x (apL (,) xs ys) | |
tree2List :: Tree a -> FunList a | |
tree2List (OneT x) = OneL x | |
tree2List (Ap f l r) = apL f (tree2List l) (tree2List r) | |
list2Tree :: FunList a -> Tree a | |
list2Tree (OneL x) = OneT x | |
list2Tree (Cons f x xs) = Ap f (OneT x) (list2Tree xs) | |
permT :: Tree a -> [Tree a] | |
permT = map list2Tree . permL . tree2List |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment