Skip to content

Instantly share code, notes, and snippets.

@danyx23
Last active November 7, 2018 20:00
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 danyx23/54ed43f9cded518d714e55024274f0f0 to your computer and use it in GitHub Desktop.
Save danyx23/54ed43f9cded518d714e55024274f0f0 to your computer and use it in GitHub Desktop.
An exmple of a simple Haskell that has a small bug
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE DeriveGeneric #-}
-- This file will not compile - there is a small error you need to fix.
-- Follow the guidelines about how to set up a new stack project and copy
-- this file, then run stack build to try and compile.
-- To have the dependencies available you have to add this fragment to the package.yaml of
-- your stack project:
-- - aeson
-- - bytestring
module Main where
import Prelude hiding (readFile)
import Data.ByteString.Lazy (readFile)
import Data.Aeson
import GHC.Generics
data Person = MkPerson {
firstName :: String
, lastName :: String
, interests :: [String]
}
deriving (Show, Eq, Generic)
instance ToJSON Person
instance FromJSON Person
readAndDecode :: FilePath -> (Either String Person)
readAndDecode path = do
content <- readFile path
return (eitherDecode content :: Either String Person)
main = do
result <- readAndDecode "test.json"
putStrLn (show result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment