diff options
| author | chirvasitua <stuart-little@users.noreply.github.com> | 2021-01-07 18:56:45 -0500 |
|---|---|---|
| committer | chirvasitua <stuart-little@users.noreply.github.com> | 2021-01-07 18:56:45 -0500 |
| commit | 6d0559b4e42ab47d2aa22fa08a22013458b29c9a (patch) | |
| tree | 87f72e9893b9a092c9cde865a3f61261a9c7df3e /challenge-042 | |
| parent | 3e42a8e7ab9f8e43eed7f07a3f76e3e8ab3e561a (diff) | |
| download | perlweeklychallenge-club-6d0559b4e42ab47d2aa22fa08a22013458b29c9a.tar.gz perlweeklychallenge-club-6d0559b4e42ab47d2aa22fa08a22013458b29c9a.tar.bz2 perlweeklychallenge-club-6d0559b4e42ab47d2aa22fa08a22013458b29c9a.zip | |
1st commit on 042_haskell
Diffstat (limited to 'challenge-042')
| -rwxr-xr-x | challenge-042/stuart-little/haskell/ch-1.hs | 12 | ||||
| -rwxr-xr-x | challenge-042/stuart-little/haskell/ch-2.hs | 20 |
2 files changed, 32 insertions, 0 deletions
diff --git a/challenge-042/stuart-little/haskell/ch-1.hs b/challenge-042/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..937f58adde --- /dev/null +++ b/challenge-042/stuart-little/haskell/ch-1.hs @@ -0,0 +1,12 @@ +#!/usr/bin/env runghc + +-- run <script> + +convToBase b nr + |nr < b =[nr] + |otherwise = (convToBase b high) ++ [low] where + (high,low) = divMod nr b + +main = do + let l = [1..50] + mapM_ putStrLn $ map (\(x,y) -> "Decimal: " ++ (show x) ++ " -- Octal: " ++ (concat $ map show y)) $ zip l $ map (convToBase 8) l diff --git a/challenge-042/stuart-little/haskell/ch-2.hs b/challenge-042/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..87996c398d --- /dev/null +++ b/challenge-042/stuart-little/haskell/ch-2.hs @@ -0,0 +1,20 @@ +#!/usr/bin/env runghc + +-- run <script> <quoted string of parentheses or nothing; if you provide nothing a string will be generated randomly> + +import System.Environment (getArgs,) +import Data.List (inits,) +import Data.List.Extra (lastDef,headDef,) +import System.Random (getStdGen,randomRIO,randomRs,) + +validate str = + let f '(' = 1; f ')' = -1; g x = map sum $ (map.map) f $ inits x + in ((lastDef 0 $ g str) == 0) && (all (0<=) $ g str) + +main = do + args <- getArgs + nrchars <- randomRIO (1::Int,50) + gen <- getStdGen + let strng = headDef (take (nrchars) (randomRs ('(',')') gen)) args + putStrLn ("Your string: " ++ strng) + putStrLn $ show $ validate strng |
