aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-11-22 23:33:51 +1100
committerMyoungjin JEON <jeongoon@gmail.com>2020-11-22 23:33:51 +1100
commit042ceb4a26cbd64d4acd66b34175c52edf8a48cb (patch)
tree9ed43d0c8ccf1921738d402cf4861e09f90ff1ee
parentd3484c804c5a1bb9aad65259d0f6d9f44acf8169 (diff)
downloadperlweeklychallenge-club-042ceb4a26cbd64d4acd66b34175c52edf8a48cb.tar.gz
perlweeklychallenge-club-042ceb4a26cbd64d4acd66b34175c52edf8a48cb.tar.bz2
perlweeklychallenge-club-042ceb4a26cbd64d4acd66b34175c52edf8a48cb.zip
[ch-087/jeongoon] Haskell Task #1
-rw-r--r--challenge-087/jeongoon/haskell/ch-1.hs32
1 files changed, 32 insertions, 0 deletions
diff --git a/challenge-087/jeongoon/haskell/ch-1.hs b/challenge-087/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..1ae687d6e5
--- /dev/null
+++ b/challenge-087/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,32 @@
+import System.Environment
+import System.Exit
+import Data.Char (isNumber)
+import Data.Maybe (catMaybes)
+import Data.List (sort, sortBy)
+
+{- tested with: runhaskell ch-1.hs 1 3 2 -1 -2 -3 6 7 9 1
+(only shows one answer)
+-}
+
+
+answerLongestConsecutiveSequence :: [Int] -> [Int]
+answerLongestConsecutiveSequence =
+ ( head
+ . sortBy (\a b -> (length b) `compare` (length a))
+ . consecutiveSequences )
+
+ where
+ consecutiveSequences =
+ ( scanl1 (\a b -> if (head b)-(last a) == 1 then (a++b) else b)
+ . map (:[])
+ . sort )
+
+main = do
+ (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs
+ >>= (\nums ->
+ if length nums < 1 then
+ die "Usage: runhaskell ch-1.hs <integer> ..."
+ else
+ putStrLn $ show ( answerLongestConsecutiveSequence nums ) )