aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-04-19 23:29:07 +0100
committerGitHub <noreply@github.com>2021-04-19 23:29:07 +0100
commitd5347857eb5cfa8aa9d34edf975c3d10b55440d3 (patch)
tree630c4cb56133b11d7114556aa0e24892388d4be1
parent78b641b69a5133a9c8f79462f9ccda0f2f5b8595 (diff)
parent17a8839d8b9fb449c3afefaaef07bf9f30dc968c (diff)
downloadperlweeklychallenge-club-d5347857eb5cfa8aa9d34edf975c3d10b55440d3.tar.gz
perlweeklychallenge-club-d5347857eb5cfa8aa9d34edf975c3d10b55440d3.tar.bz2
perlweeklychallenge-club-d5347857eb5cfa8aa9d34edf975c3d10b55440d3.zip
Merge pull request #3931 from stuart-little/stuart-little_109_haskell
1st commit on 109_haskell
-rwxr-xr-xchallenge-109/stuart-little/haskell/ch-1.hs6
-rwxr-xr-xchallenge-109/stuart-little/haskell/ch-2.hs25
2 files changed, 31 insertions, 0 deletions
diff --git a/challenge-109/stuart-little/haskell/ch-1.hs b/challenge-109/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..3de34c1573
--- /dev/null
+++ b/challenge-109/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,6 @@
+#!/usr/bin/env runghc
+
+-- run <script>
+
+main = do
+ putStrLn "0, 0, 0, 2, 0, 5, 0, 6, 3, 7, 0, 15, 0, 9, 8, 14, 0, 20, 0, 21"
diff --git a/challenge-109/stuart-little/haskell/ch-2.hs b/challenge-109/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..5c203090ec
--- /dev/null
+++ b/challenge-109/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,25 @@
+#!/usr/bin/env runghc
+
+-- run <script> <space-separated numbers>
+
+import Data.List (intercalate,permutations)
+import System.Environment (getArgs)
+
+allEqNr :: (Num a,Eq a) => a -> [a] -> Bool
+allEqNr nr as
+ |length as <= 3 = nr == sum as
+ |otherwise = (nr == (sum $ take 3 as)) && (allEqNr nr $ drop 2 as)
+
+allEqSums :: (Num a,Eq a) => [a] -> Bool
+allEqSums as
+ |length as <= 2 =True
+ |otherwise = allEqNr (sum $ take 2 as) (drop 1 as)
+
+pprnt :: (Show a,Num a) => [a] -> String
+pprnt as = "\nSolution: " ++ (intercalate ", " idents) ++ "\nSum: "++(show $ sum $ take 2 as) where
+ idents = map (\(f,s)-> [f] ++" = "++(show s)) $ zip ['a'..'g'] as
+
+main = do
+ args <- getArgs >>= return . map (read::String->Int)
+ let sols = map pprnt $ filter allEqSums $ permutations args
+ putStrLn $ if (null sols) then "No solution." else unwords sols