diff options
author | Determinant <ted.sybil@gmail.com> | 2017-06-08 13:51:43 -0400 |
---|---|---|
committer | Determinant <ted.sybil@gmail.com> | 2017-06-08 13:51:43 -0400 |
commit | 569664d524e4772342752f863778fe2c869a822a (patch) | |
tree | 99cde922b6b0e06b8a7637bc9ef09f46af0f49e8 /90-94/93.hs | |
parent | 2da9fcf44fb1e403860535e8b165434036d50540 (diff) |
finish vol 10
Diffstat (limited to '90-94/93.hs')
-rw-r--r-- | 90-94/93.hs | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/90-94/93.hs b/90-94/93.hs new file mode 100644 index 0000000..c031e7b --- /dev/null +++ b/90-94/93.hs @@ -0,0 +1,34 @@ +puzzle :: [Integer] -> [String] + +puzzle l = do i <- [1..length l-1] + let (subl, subr) = splitAt i l + (sl, vl, _) <- gen subl + (sr, vr, _) <- gen subr + if vl == vr then + return (sl ++ " = " ++ sr) + else [] + +gen :: [Integer] -> [(String, Rational, String)] +gen (x:[]) = return (show x, fromInteger x, "_") + +gen l = do i <- [1..length l-1] + let (subl, subr) = splitAt i l + (sl, vl, opsl) <- gen subl + (sr, vr, opsr) <- gen subr + (ops, op) <- [("+", (+)), ("-", (-)), ("*", (*)), ("/", (/))] + if (ops == "/" && vr == 0) || + (ops == "+" && (opsr == "+" || opsr == "-")) || + (ops == "*" && (opsr == "*" || opsr == "/")) then [] + else + return ((if opsl /= "_" && + (ops == "*" || ops == "/") && + (opsl == "+" || opsl == "-") then + "(" ++ sl ++ ")" + else sl) + ++ " " ++ ops ++ " " ++ + (if opsr /= "_" && + ((ops == "-" && opsr /= "*" && opsr /= "/") || + (ops == "*" && (opsr == "+" || opsr == "-")) || + ops == "/") then + "(" ++ sr ++ ")" + else sr), op vl vr, ops) |