aboutsummaryrefslogtreecommitdiff
path: root/challenge-086
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-11-25 22:27:51 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-11-25 22:27:51 +1100
commit6bebdc5ac2c140f354a90face0ddf5739e1f7e84 (patch)
tree9b7e01901ea0216b721ac66e8394805ab14e99a8 /challenge-086
parentf3b73a726dfa294a9e585ed93284d41498a1a0e0 (diff)
downloadperlweeklychallenge-club-6bebdc5ac2c140f354a90face0ddf5739e1f7e84.tar.gz
perlweeklychallenge-club-6bebdc5ac2c140f354a90face0ddf5739e1f7e84.tar.bz2
perlweeklychallenge-club-6bebdc5ac2c140f354a90face0ddf5739e1f7e84.zip
[ch-086/jeongoon] ch-1.hs added
Diffstat (limited to 'challenge-086')
-rw-r--r--challenge-086/jeongoon/haskell/ch-1.hs41
1 files changed, 41 insertions, 0 deletions
diff --git a/challenge-086/jeongoon/haskell/ch-1.hs b/challenge-086/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..235f84e555
--- /dev/null
+++ b/challenge-086/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,41 @@
+import System.Environment
+import System.Exit
+import Data.Char (isNumber)
+import Data.Maybe (isNothing, catMaybes)
+import Data.List (sort)
+
+{- tested with:
+runhaskell ch-1.hs 1 3 2 -1 -2 -3 6 7 9 1
+1 as 3 -2 = 1
+-}
+
+usageMessage = "Usage: runhaskell ch-1.hs "
+ ++ "<target diff(positive integer)> <integer> ..."
+
+pairDifference _ [] = Nothing
+pairDifference target ls = pairdiff target (head sorted) (tail sorted) 1
+ where
+ sorted = sort ls
+ pairdiff _ _ [] _ = Nothing
+ pairdiff t f rst@(r:_) i = -- t: target, f: first, i: index
+ if i >= (length rst) then Nothing
+ else case ((rst !! i) - f) `compare` t of
+ LT -> pairdiff t f rst (succ i)
+ GT -> pairdiff t r rst 1
+ EQ -> Just (f, (rst !! i))
+
+main = do
+ ints <- (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs;
+
+ let d = (head ints) in
+
+ if (length ints) < 2
+ then die usageMessage
+ else if d < 0
+ then die usageMessage
+ else case pairDifference d (tail ints) of
+ Nothing -> putStrLn "0"
+ Just (a,b) -> putStrLn $ "1 as " ++ (show b) ++ " - "
+ ++ (show a) ++ " = " ++ (show d)