The source code below was auto-fetched from: https://gitweb.softwarefools.com/?p=miguel/haskell.git;a=blob;f=snip/snip.hs
import Control.Monad.Writer
import Control.Monad.State
import Data.Tuple.Extra (dupe,first,second)
import System.Environment (getArgs)
import Control.Parallel.Strategies (parMap,rseq,rdeepseq)
main = do
mainMonTra
mainParMap
-- monad transformers in action
mainMonTra = print $
runState (
runWriterT (
get >>= \a ->
tell ["foo"] >>
put (a*a) >>
tell ["bar"] >>
tell [show a]
)
) 5
-- parallel Map arguments: sparks maps
mainParMap = do
(sparks:max:_) <- fmap (map read) getArgs
let ranges = uncurry zip . second tail . dupe $ [0,(max`div`sparks)..max]
let sums = parMap rdeepseq (\(a,b)->sum [a+1..b]) ranges
print $ show (sum sums::Integer)
You can also use haskell in conjunction with a shebang line as illustrated below:
The source code below was auto-fetched from: https://gitweb.softwarefools.com/?p=miguel/haskell.git;a=blob;f=snip/shebang.hs
#!/usr/bin/env stack
-- stack --resolver lts-12.5 script
{-# LANGUAGE OverloadedStrings #-}
import qualified Data.ByteString.Lazy.Char8 as L8
import Network.HTTP.Simple
-- https://haskell-lang.org/tutorial/stack-script
main :: IO ()
main = do
response <- httpLBS "http://httpbin.org/get"
putStrLn $ "The status code was: " ++
show (getResponseStatusCode response)
print $ getResponseHeader "Content-Type" response
L8.putStrLn $ getResponseBody response