Skip to content

Instantly share code, notes, and snippets.

@kana
Created February 6, 2011 12:06
Show Gist options
  • Save kana/813326 to your computer and use it in GitHub Desktop.
Save kana/813326 to your computer and use it in GitHub Desktop.
Reviewing Haskell by writing Unix commands
import System.Time
import System.Locale
main = getClockTime >>=
toCalendarTime >>=
putStrLn . formatCalendarTime defaultTimeLocale "%c"
import System.Environment
main = getArgs >>= putStrLn . unwords
import System.Exit
main = exitFailure
import System.Directory
main = getCurrentDirectory >>= putStrLn
import Data.List
main = getContents >>= putStrLn . unlines . sort . lines
import System.Environment
main = getArgs >>= \args -> (
getContents >>= \s -> (
tee args s
)
)
tee :: [String] -> String -> IO ()
tee [] s = putStr s
tee (filename:rest) s = writeFile filename s >> tee rest s
import System.Exit
main = exitWith ExitSuccess
main = getContents >>= \x -> putStrLn $ unlines $ uniq $ lines x
uniq :: (Eq a) => [a] -> [a]
uniq [] = []
uniq (x:[]) = [x]
uniq (x:xs) = if x == head xs then uniq xs else x:(uniq xs)
import System.Environment
main = getArgs >>= putStrLn . yes
yes :: [String] -> String
yes xs = unlines $ repeat $ unwords $ normalizeArguments xs
normalizeArguments :: [String] -> [String]
normalizeArguments [] = ["y"]
normalizeArguments xs = xs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment