Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scripting in Haskell

Scripting in Haskell

source: https://www.youtube.com/watch?v=UVek-DEc_pQ

Preliminar notes

Haskell is a pure functional programming language, for more information visit: https://www.haskell.org/

Stack is a build tool for haskell, make reproducible builds for haskell using isolated environments with , and also it is a scripting tool. More info at https://www.haskellstack.org/.

A script is an unversioned program, don't need packeging, solve an very especific problem in a narrow way, is reproduceble (the same piece of code works the same way over the years).

In haskell, scripts are multiplatform, the same source works on Windows, Linux and MacOS.

Hello World

#!/usr/bin/env stack
-- stack runghc --resolver lts-8.5 --install-ghc
main = do
  putStrLn "Hello, World!"
  putStrLn "Hi!"
  • The first line is a shebang line like any scripting language.
  • The second line is a specific to stack calling to run runghc using an especific resolver and try install the compiler for that particular resolver.
  • The following lines are a simple haskell program.

Histogram

This is the second example on the video. The program reads a text file and creates a histogram using the words lenght.

#!/usr/bin/env stack
-- stack runghc --resolver lts-8.5 --install-ghc
import           Data.Char
import           Data.List
import qualified Data.Text.IO as T
import qualified Data.Text    as T
import qualified Data.Map.Strict as M
import           System.Environment
main = do
  [fp] <- getArgs
  text <- T.readFile fp
  let histo =
        foldl'
          (\hash line -> M.insertWith (+) (T.length line) 1 hash)
          M.empty
          (T.lines text)
  mapM_ (\(key, value) -> print (key, value))
        (M.toList histo)

FSNotify

#!/usr/bin/env stack
-- stack runghc --resolver lts-8.5 --install-ghc --package fsnotify
{-# LANGUAGE OverloadedStrings #-} -- for FilePath literals

import System.FSNotify
import Control.Concurrent (threadDelay)
import Control.Monad      (forever)

main = 
  withManager $ \mgr -> do
    -- start a watching job (in the background)
    watchDir
      mgr          -- manager
      "."          -- directory to watch
      (const True) -- predicate
      print        -- action
    
    -- sleep forever (until interrupted)
    forever $ threadDelay 1000000

thanks to www.fpcomplete.com

#!/usr/bin/env stack
-- stack runghc --resolver lts-8.5 --install-ghc --package fsnotify
{-# LANGUAGE OverloadedStrings #-} -- for FilePath literals
import System.FSNotify
import Control.Concurrent (threadDelay)
import Control.Monad (forever)
main =
withManager $ \mgr -> do
-- start a watching job (in the background)
watchDir
mgr -- manager
"." -- directory to watch
(const True) -- predicate
print -- action
-- sleep forever (until interrupted)
forever $ threadDelay 1000000
#!/usr/bin/env stack
-- stack runghc --resolver lts-8.5 --install-ghc
main = do
putStrLn "Hello, World!"
putStrLn "Hi!"
#!/usr/bin/env stack
-- stack runghc --resolver lts-8.5 --install-ghc
import Data.Char
import Data.List
import qualified Data.Text.IO as T
import qualified Data.Text as T
import qualified Data.Map.Strict as M
import System.Environment
main = do
[fp] <- getArgs
text <- T.readFile fp
let histo =
foldl'
(\hash line -> M.insertWith (+) (T.length line) 1 hash)
M.empty
(T.lines text)
mapM_ (\(key, value) -> print (key, value))
(M.toList histo)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.