Skip to content

Instantly share code, notes, and snippets.

@djhworld
Created June 2, 2011 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save djhworld/1005383 to your computer and use it in GitHub Desktop.
Save djhworld/1005383 to your computer and use it in GitHub Desktop.
Haskell One Liners
-- Inspired by http://solog.co/47/10-scala-one-liners-to-impress-your-friends/
-- Double everything in a list
map (*2) [1..10]
-- Sum a list of numbers
sum [1..1000]
-- Verify if exists in a string (thanks to hammar for this: http://stackoverflow.com/questions/6224315/how-to-verify-if-some-items-are-in-a-list)
any (`elem` ["haskell", "ghc", "monads", "cabal"]) $ words "this is a piece of example text talking about haskell and ghc"
-- read in a file
readFile "file.txt" >>= return . lines
-- Sing happy birthday
putStr "Who's the birthday boy then? > " >> getLine >>= (\name -> mapM_ (\i -> putStrLn $ "Happy Birthday" ++ (if (i == 3) then " dear " ++ name else " to you!")) [1..4])
-- Filter/partiton a list of numbers
partition (>60) [49, 58, 76, 82, 88, 90]
-- Fetch and Parse an XML web service
-- Find a minimum or maximum in a list
maximum [14, 35, -7, 46, 98]
minimum [14, 35, -7, 46, 98]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment