[Assorted-commits] SF.net SVN: assorted: [865] sandbox/trunk/src/hs
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-07-02 19:50:11
|
Revision: 865 http://assorted.svn.sourceforge.net/assorted/?rev=865&view=rev Author: yangzhang Date: 2008-07-02 12:50:18 -0700 (Wed, 02 Jul 2008) Log Message: ----------- added newbie normalization programs Added Paths: ----------- sandbox/trunk/src/hs/Normalize0.lhs sandbox/trunk/src/hs/Normalize1.lhs sandbox/trunk/src/hs/Normalize2.lhs Added: sandbox/trunk/src/hs/Normalize0.lhs =================================================================== --- sandbox/trunk/src/hs/Normalize0.lhs (rev 0) +++ sandbox/trunk/src/hs/Normalize0.lhs 2008-07-02 19:50:18 UTC (rev 865) @@ -0,0 +1,46 @@ +#!/usr/bin/env runhaskell + +This is one of my first attempts at writing Haskell. This sandbox contains +three iterations of this program, which I went through while taking suggestions +from people in #haskell. + +For each line from stdin, normalize the second field +against the value specified on the command line. + +E.g., for input: + 1 200 lowest + 2 400 highest + 3 300 + 4 400 + +normalize should output: + 1 0.5 lowest + 2 1.0 highest + 3 0.75 + 4 1.0 + +> import System (getArgs) +> +> main = do +> argv <- getArgs +> interact $ (processLines (argv !! 0) (argv !! 1) (argv !! 2)) . lines +> -- interact $ (uncurry processLines (take 3 argv)) . lines +> -- getArgs >>= \argv -> (interact $ unlines . (processLines (head argv)) . lines) +> +> processLines operation max1 max2 ls = if operation == "max" +> then show getMax +> else unlines $ map normLine ls +> where normLine = unwords . normWords . words +> normLine :: String -> String +> normWords ws = [head ws, normNumber ws] ++ (getRest ws) +> normWords :: [String] -> [String] +> getNumber = read . head . tail +> getNumber :: [String] -> Double +> getRest = drop 2 +> getRest :: [a] -> [a] +> normNumber = show . (/ maxNumber) . getNumber +> normNumber :: [String] -> String +> maxNumber = max (read max1) (read max2) +> maxNumber :: Double +> getMax = maximum . (map (getNumber . words)) $ ls +> getMax :: Double Added: sandbox/trunk/src/hs/Normalize1.lhs =================================================================== --- sandbox/trunk/src/hs/Normalize1.lhs (rev 0) +++ sandbox/trunk/src/hs/Normalize1.lhs 2008-07-02 19:50:18 UTC (rev 865) @@ -0,0 +1,35 @@ +#!/usr/bin/env runhaskell + +For each line from stdin, normalize the second (0-indexed) field +against the value specified on the command line. + +E.g., for input: + 1 200 lowest + 2 400 highest + 3 300 + 4 400 + +normalize 1 400 should output: + 1 0.5 lowest + 2 1.0 highest + 3 0.75 + 4 1.0 + +> import System (getArgs) +> +> main = do +> argv <- getArgs +> interact $ (processLines (read (argv !! 0)) (read (argv !! 1))) . lines +> +> processLines :: Int -> Float -> [String] -> String +> processLines index maxNumber ls = unlines $ map normLine ls +> where normLine :: String -> String +> normLine = unwords . normWords . words +> normWords :: [String] -> [String] +> normWords ws = before ++ middle ++ after +> where before :: [String] +> before = take index ws +> middle :: [String] +> middle = [show . (/ maxNumber) . read $ ws !! index] +> after :: [String] +> after = drop (index + 1) ws Added: sandbox/trunk/src/hs/Normalize2.lhs =================================================================== --- sandbox/trunk/src/hs/Normalize2.lhs (rev 0) +++ sandbox/trunk/src/hs/Normalize2.lhs 2008-07-02 19:50:18 UTC (rev 865) @@ -0,0 +1,35 @@ +#!/usr/bin/env runhaskell + +For each line from stdin, normalize the second (0-indexed) field +against the value specified on the command line. + +E.g., for input: + 1 200 lowest + 2 400 highest + 3 300 + 4 400 + +normalize 1 400 should output: + 1 0.5 lowest + 2 1.0 highest + 3 0.75 + 4 1.0 + +> import System (getArgs) +> +> main = do +> [arg0, arg1] <- getArgs +> interact $ normalize (read arg0) (read arg1) +> +> normalize :: Int -> Float -> String -> String +> normalize index maxNumber = processLines (processField index (/ maxNumber)) +> +> processLines :: ([[String]] -> [[String]]) -> String -> String +> processLines f = unlines . (map unwords) . f . (map words) . lines +> +> processField :: (Read a, Show a) => Int -> (a -> a) -> [[String]] -> [[String]] +> processField index f = map (mapAt index (show . f . read)) +> +> mapAt :: Int -> (a -> a) -> [a] -> [a] +> mapAt index f ws = before ++ (f middle) : after +> where (before, middle : after) = splitAt index ws This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |