{-# OPTIONS -Wall -fno-warn-type-defaults -XMultiParamTypeClasses #-}
module Classes where
import Test.HUnit
import Data.Char
import Prelude hiding (Maybe,Just,Nothing,Either,Left,Right,lookup)
data Maybe a = Nothing | Just a
justThree :: Maybe Int
justThree = Just 3
noInt :: Maybe Int
noInt = Nothing
justTrue :: Maybe Bool
justTrue = Just True
data Either a b = Left a | Right b
safeDiv :: Int -> Int -> Either String Int
safeDiv _ 0 = Left "You can't divide by zero, silly."
safeDiv x y = Right $ x `div` y
data Tree a = Leaf | Branch a (Tree a) (Tree a)
exTree :: Tree Int
exTree = Branch 5 (Branch 2 (Branch 1 Leaf Leaf) (Branch 4 Leaf Leaf))
(Branch 9 Leaf (Branch 7 Leaf Leaf))
treePlus :: Tree Int -> Int -> Tree Int
treePlus = undefined
infixOrder :: Tree a -> [a]
infixOrder = undefined
treeMap :: (a -> b) -> Tree a -> Tree b
treeMap = undefined
treeIncr :: Tree Int -> Tree Int
treeIncr = treeMap (+1)
treeFold :: a
treeFold = undefined
infixOrder' :: Tree a -> [a]
infixOrder' = treeFold undefined []
prefixOrder :: Tree a -> [a]
prefixOrder = treeFold undefined []
postfixOrder :: Tree a -> [a]
postfixOrder = treeFold undefined []
fancySeven :: Int
fancySeven = 3 + 4
fancyEight :: Float
fancyEight = 3.2 + 4.8
data PrimaryColor = Red | Green | Blue
instance Eq PrimaryColor where
Red == Red = True
Green == Green = True
Blue == Blue = True
_ == _ = False
c1 /= c2 = not (c1 == c2)
fancyTrue :: Bool
fancyTrue = Red == Red
instance Eq a => Eq (Tree a) where
_ == _ = undefined
tree1, tree2 :: Tree Int
tree1 = Branch 3 (Branch 2 Leaf Leaf) (Branch 1 Leaf Leaf)
tree2 = Branch 3 Leaf Leaf
treeEqTest :: Test
treeEqTest = TestList [ tree1 == tree1 ~?= True,
tree1 == tree2 ~?= False,
tree1 == Leaf ~?= False ]
lookup :: Eq a => a -> [(a,b)] -> Maybe b
lookup _ [] = Nothing
lookup a ((a',b):ps) = if a == a' then Just b
else lookup a ps
data Shape = Circle Float Float Float
| Rectangle Float Float Float Float
deriving (Eq)
data IntFunctions = OneArg (Int -> Int)
| TwoArg (Int -> Int -> Int)
data SadColors = Black | Brown | Grey
deriving (Eq, Show, Read)
class Convertible a b where
convert :: a -> b
instance Convertible Char Int where
convert = ord
instance Convertible (Tree a) [a] where
convert = infixOrder
tenToThirty :: [Int]
tenToThirty = [10..30]
abcde :: [Char]
abcde = ['a'..'e']
biggestInt :: Int
biggestInt = maxBound
instance Functor Tree where
-- fmap :: (a -> b) -> Tree a -> Tree b
fmap = treeMap
main :: IO ()
main = putStrLn "This is Classes"