Skip to content

Instantly share code, notes, and snippets.

@j1r1k
Created May 16, 2016 18:44
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 j1r1k/7ccc3e0996295370b8965c0f3c882577 to your computer and use it in GitHub Desktop.
Save j1r1k/7ccc3e0996295370b8965c0f3c882577 to your computer and use it in GitHub Desktop.
PureScript by Example: Chapter 10 (The Foreign Function Interface) exercise 10.19.5
module Tree where
import Prelude
import Control.Alt
import Data.Either
import Data.Foreign
import Data.Foreign.Class
data Tree a = Leaf a | Branch (Tree a) (Tree a)
instance treeIsForeign :: (IsForeign a) => IsForeign (Tree a) where
read value = (Branch <$> readProp "left" value <*> readProp "right" value)
<|> (Leaf <$> readProp "value" value)
instance showTree :: (Show a) => Show (Tree a) where
show (Branch l r) = "Tree Left [ " ++ show l ++ " ] Right [ " ++ show r ++ "]"
show (Leaf a) = "Leaf " ++ show a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment