aboutsummaryrefslogtreecommitdiff
path: root/challenge-007/stuart-little/haskell
diff options
context:
space:
mode:
authorchirvasitua <stuart-little@users.noreply.github.com>2021-01-12 16:09:36 -0500
committerchirvasitua <stuart-little@users.noreply.github.com>2021-01-12 16:09:36 -0500
commit441cfa378fc6df148a2e1ea13c77928401db51ea (patch)
treebf0982c2e04206df23a11c3a952183b7db440d23 /challenge-007/stuart-little/haskell
parentb00c8c4f9ff06c18683a5e5380b03585f3301e43 (diff)
downloadperlweeklychallenge-club-441cfa378fc6df148a2e1ea13c77928401db51ea.tar.gz
perlweeklychallenge-club-441cfa378fc6df148a2e1ea13c77928401db51ea.tar.bz2
perlweeklychallenge-club-441cfa378fc6df148a2e1ea13c77928401db51ea.zip
1st commit on 007_haskell
Diffstat (limited to 'challenge-007/stuart-little/haskell')
-rwxr-xr-xchallenge-007/stuart-little/haskell/ch-1.hs6
-rwxr-xr-xchallenge-007/stuart-little/haskell/ch-2.hs28
2 files changed, 34 insertions, 0 deletions
diff --git a/challenge-007/stuart-little/haskell/ch-1.hs b/challenge-007/stuart-little/haskell/ch-1.hs
new file mode 100755
index 0000000000..70c993827a
--- /dev/null
+++ b/challenge-007/stuart-little/haskell/ch-1.hs
@@ -0,0 +1,6 @@
+#!/usr/bin/env runghc
+
+-- run <script>
+
+main = do
+ mapM_ print $ let digSum x = sum $ map (\d-> read [d]::Int) $ show x in (0:) $ filter (\x -> mod x (digSum x) == 0) [1..50]
diff --git a/challenge-007/stuart-little/haskell/ch-2.hs b/challenge-007/stuart-little/haskell/ch-2.hs
new file mode 100755
index 0000000000..4ff8c03e56
--- /dev/null
+++ b/challenge-007/stuart-little/haskell/ch-2.hs
@@ -0,0 +1,28 @@
+#!/usr/bin/env runghc
+
+-- run <script> <word1> <word2> <path-to-file>
+
+import System.Environment (getArgs,)
+import Data.List (delete,)
+import Data.List.Extra (minimumOn,notNull,)
+import Math.TreeFun.Tree (boolToInt,)
+import Control.Monad (liftM,)
+
+dist1 :: Eq a => [a] -> [a] -> Bool
+dist1 xs ys = ((length xs) == (length ys)) && ((==) 1 $ sum $ map boolToInt $ zipWith (/=) xs ys)
+
+shortestLadder :: String -> String -> [String] -> [String]
+shortestLadder w1 w2 ws
+ |(notElem w1 ws) || (notElem w2 ws) =[]
+ |w1 == w2 =[w1]
+ |otherwise = let
+ wsshort = delete w1 ws
+ l = map (w1:) $ filter (notNull) $ map (\w -> shortestLadder w w2 wsshort) $ filter (dist1 w1) wsshort
+ in
+ if (null l) then []
+ else (minimumOn length l)
+
+main = do
+ (w1:w2:file:_) <- getArgs
+ ws <- liftM words $ readFile file
+ putStrLn $ unwords $ shortestLadder w1 w2 ws