aboutsummaryrefslogtreecommitdiff
path: root/challenge-086
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-11-29 18:09:11 +0000
committerGitHub <noreply@github.com>2020-11-29 18:09:11 +0000
commit2d77a41a573354f71849ccc627eac0ac834805f4 (patch)
treee6a5d4d5a99fa3670ab9d171ec6991a397ecb5ee /challenge-086
parent8f4c1b97453d02492a7738b1f0cb6c7358af4ae9 (diff)
parent8178fe5e04e95cfbc20c8bb2d4210bc848cb76af (diff)
downloadperlweeklychallenge-club-2d77a41a573354f71849ccc627eac0ac834805f4.tar.gz
perlweeklychallenge-club-2d77a41a573354f71849ccc627eac0ac834805f4.tar.bz2
perlweeklychallenge-club-2d77a41a573354f71849ccc627eac0ac834805f4.zip
Merge pull request #2866 from jeongoon/master
[ch-088/jeongoon] Perl, Raku, Haskell, Go, Common-lisp, Elm solution added.
Diffstat (limited to 'challenge-086')
-rw-r--r--challenge-086/jeongoon/haskell/ch-1.hs42
1 files changed, 42 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..59a46bb9ec
--- /dev/null
+++ b/challenge-086/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,42 @@
+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
+ (catMaybes.map (\nStr ->
+ -- poor parser
+ if (all (`elem` "0123456789+-") nStr)
+ then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs
+ >>= ( \ints ->
+ 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) )