Last active
November 7, 2018 20:00
-
-
Save danyx23/54ed43f9cded518d714e55024274f0f0 to your computer and use it in GitHub Desktop.
An exmple of a simple Haskell that has a small bug
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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