Skip to content

Instantly share code, notes, and snippets.

@varreli
Created May 11, 2018 23:14
Show Gist options
  • Save varreli/eabdbfa6edb6241d101b16280961fef3 to your computer and use it in GitHub Desktop.
Save varreli/eabdbfa6edb6241d101b16280961fef3 to your computer and use it in GitHub Desktop.
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