Created
May 11, 2018 23:14
-
-
Save varreli/eabdbfa6edb6241d101b16280961fef3 to your computer and use it in GitHub Desktop.
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
split :: [a] -> [([a], [a])] | |
split [] = [] | |
split [_] = [] | |
split (x:xs) = ([x], xs) : [(x:ls, rs) | (ls, rs) <- split xs] | |
-- auxillary function combine in exprs: | |
combine :: Expr -> Expr -> [Expr] | |
combine l r = [App o l r | o <- ops] | |
ops :: [Op] | |
ops = [Add,Sub,Mul,Div] | |
--------------------------------------- | |
exprs :: [Int] -> [Expr] | |
exprs [] = [] | |
exprs [n] = [Val n] | |
exprs ns = [ e | (ls, rs) <- split ns, | |
l <- exprs ls, | |
r <- exprs rs, | |
e <- combine l r] | |
solutions :: [Int] -> Int -> [Expr] | |
solutions ns n = | |
[e | ns' <- choices ns, e <- exprs ns', eval e == [n]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment