Skip to content

Instantly share code, notes, and snippets.

@BernardNotarianni
Last active December 23, 2018 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BernardNotarianni/05463cbd9b0c67ba24872ad13808ebff to your computer and use it in GitHub Desktop.
Save BernardNotarianni/05463cbd9b0c67ba24872ad13808ebff to your computer and use it in GitHub Desktop.
Haskell Recipes

Haskell recipes

Read and print to console

main :: IO ()
main = do
  putStrLn("Hello! what is your name?")
  name <- getLine
  putStrLn("Hello " ++ name ++ "!")

Print content of a file

import System.IO

main :: IO ()
main = do
  content <- readFile "hello.txt"
  putStrLn("Content of the file:")
  putStrLn(content)

Reverse content of a file and print it

import System.IO

main :: IO ()
main = do
  content <- readFile "hello.txt"
  putStrLn("Reverse content of the file:")
  let rev = reverse content
  putStrLn(rev)

Read, parse and print json from file

{-# LANGUAGE DeriveGeneric #-}

module Main where

import           System.IO

import           Data.Aeson
import qualified Data.ByteString.Lazy as L
import qualified Data.Text            as T
import           GHC.Generics

data Person = Person
              { name      :: T.Text
              , firstname :: T.Text
              , age       :: Int
              } deriving (Show, Generic)

instance FromJSON Person
instance ToJSON Person


main :: IO ()
main = do
  content <- L.readFile "persons.json"
  let persons = decode content :: Maybe [ Person ]
  putStrLn("Content of the file:")
  putStrLn(show persons)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment