diff options
| -rwxr-xr-x | challenge-009/stuart-little/haskell/ch-1.hs | 9 | ||||
| -rwxr-xr-x | challenge-009/stuart-little/haskell/ch-2.hs | 32 |
2 files changed, 41 insertions, 0 deletions
diff --git a/challenge-009/stuart-little/haskell/ch-1.hs b/challenge-009/stuart-little/haskell/ch-1.hs new file mode 100755 index 0000000000..18bd6437e0 --- /dev/null +++ b/challenge-009/stuart-little/haskell/ch-1.hs @@ -0,0 +1,9 @@ +#!/usr/bin/env runghc + +-- run <script> + +import Data.List (transpose,nub,) + +main = do + putStrLn ((show root) ++ " squares to " ++ (show sq)) where + (root,sq) = head $ dropWhile ((4 >=).length.(\x -> nub $ transpose [x] ).show.snd) $ zip [1..] $ map (\x -> x*x) [1..] diff --git a/challenge-009/stuart-little/haskell/ch-2.hs b/challenge-009/stuart-little/haskell/ch-2.hs new file mode 100755 index 0000000000..4042e1346c --- /dev/null +++ b/challenge-009/stuart-little/haskell/ch-2.hs @@ -0,0 +1,32 @@ +#!/usr/bin/env runghc + +-- run <script> <space-separated numbers> + +import System.Environment (getArgs,) +import Data.List (sort,nub,group,) + +rankWith :: (Eq a, Ord a) => ([Int] -> [Int]) -> [a] -> [(a,Int)] +rankWith tally xs = zip (nub sorted) (tally $ map length $ group $ sorted) where + sorted = sort xs + +rankStd :: (Eq a, Ord a) => [a] -> [(a,Int)] +rankStd = rankWith (scanl (+) 1) + +rankMod :: (Eq a, Ord a) => [a] -> [(a,Int)] +rankMod = rankWith (scanl1 (+)) + +rankDense :: (Eq a, Ord a) => [a] -> [(a,Int)] +rankDense = rankWith ((scanl1 (+)).(map (const 1))) + +pp :: Show a => [(a,Int)] -> [String] +pp xs = map (\(n,r) -> ((show n) ++ " -> Rank " ++ (show r))) xs + +main = do + args <- getArgs + let argsnum = map (read::String->Int) args + putStrLn "Standard Ranking:" + mapM_ putStrLn $ pp $ rankStd argsnum + putStrLn "Modified Ranking:" + mapM_ putStrLn $ pp $ rankMod argsnum + putStrLn "Dense Ranking:" + mapM_ putStrLn $ pp $ rankDense argsnum |
