I am reading making our own types and typeclasses in learn you a haskell.
In section Algebraic data types intro I notice:
data Point = Point Float Float deriving (Show) data Shape = Circle Point Float | Rectangle Point Point deriving (Show)surface :: Shape -> Floatsurface (Rectangle (Point x1 y1) (Point x2 y2)) = abs (x2 - x1) * abs (y2 - y1)
In surface (Rectangle (Point x1 y1) (Point x2 y2))
, we indicate the parameters for Rectangle are of type Point.
However, in section Recursive data structures:
data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)singleton :: a -> Tree a singleton x = Node x EmptyTree EmptyTree treeInsert :: (Ord a) => a -> Tree a -> Tree a treeInsert x EmptyTree = singleton x treeInsert x (Node a left right) | x == a = Node x left right | x < a = Node a (treeInsert x left) right | x > a = Node a left (treeInsert x right)
We don't indicate left
's and right
's data types are Tree a
in treeInsert x (Node a left right)
. How does the compiler know their type?