aboutsummaryrefslogtreecommitdiff
path: root/challenge-080/jeongoon/haskell
diff options
context:
space:
mode:
authorMyoungjin JEON <jeongoon@gmail.com>2020-10-01 11:24:18 +1000
committerMyoungjin JEON <jeongoon@gmail.com>2020-10-01 11:24:18 +1000
commita987f049bd2cea92b7e8bd7ca05d732068b69f67 (patch)
treef8f8941e6ce447f221ab8b49bcdeb852b11428a7 /challenge-080/jeongoon/haskell
parenta2e2740210f3b090c817e2e06801e36307290cef (diff)
downloadperlweeklychallenge-club-a987f049bd2cea92b7e8bd7ca05d732068b69f67.tar.gz
perlweeklychallenge-club-a987f049bd2cea92b7e8bd7ca05d732068b69f67.tar.bz2
perlweeklychallenge-club-a987f049bd2cea92b7e8bd7ca05d732068b69f67.zip
[ch-080/jeongoon] Perl, Raku, Haskell Solution added.
Diffstat (limited to 'challenge-080/jeongoon/haskell')
-rw-r--r--challenge-080/jeongoon/haskell/ch-1.hs20
-rw-r--r--challenge-080/jeongoon/haskell/ch-2.hs26
2 files changed, 46 insertions, 0 deletions
diff --git a/challenge-080/jeongoon/haskell/ch-1.hs b/challenge-080/jeongoon/haskell/ch-1.hs
new file mode 100644
index 0000000000..7809ab05b8
--- /dev/null
+++ b/challenge-080/jeongoon/haskell/ch-1.hs
@@ -0,0 +1,20 @@
+import System.Environment
+import System.Exit
+import Data.Char (isNumber)
+import Data.Maybe (isJust, catMaybes)
+import qualified Data.Set as Set
+
+-- https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/#TASK1
+{- tested with:
+runhaskell ch-1.hs 5 -2 2 0
+-}
+
+smallestPostiveNumber ints = -- result "1" with empty list
+ (head.dropWhile (isJust.((flip Set.lookupIndex) (Set.fromList ints)))) [1..]
+
+main = do
+ nums <- (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs;
+ if length nums == 0 then putStrLn "runhaskell ch-1.hs <integer> ..."
+ else (putStrLn.show) $ smallestPostiveNumber nums
diff --git a/challenge-080/jeongoon/haskell/ch-2.hs b/challenge-080/jeongoon/haskell/ch-2.hs
new file mode 100644
index 0000000000..10fd60cec0
--- /dev/null
+++ b/challenge-080/jeongoon/haskell/ch-2.hs
@@ -0,0 +1,26 @@
+import System.Environment
+import System.Exit
+import Data.List (zipWith)
+import Data.Char (isNumber)
+import Data.Maybe (isJust, catMaybes)
+
+-- https://perlweeklychallenge.org/blog/perl-weekly-challenge-080/#TASK2
+{- tested with:
+runhaskell ch-2.hs 1 4 3 2
+-}
+
+countCandies [] = 0
+countCandies ranks = defaultCandies + extraCandies
+ where
+ leftGroup = init ranks
+ rightGroup = tail ranks
+ defaultCandies = length ranks
+ extraCandies =
+ sum $ zipWith (\l r -> if l /= r then 1 else 0) leftGroup rightGroup
+
+main = do
+ nums <- (catMaybes.map (\nStr ->
+ if (all isNumber nStr) then Just(read nStr :: Int)
+ else Nothing )) `fmap` getArgs;
+ if length nums == 0 then putStrLn "runhaskell ch-1.hs <unsigned integer> ..."
+ else (putStrLn.show) $ countCandies nums