20-CS-4003-001 |
Organization of Programming Languages |
Fall 2017 |
---|---|---|

Functors |

Prev
Next
All lectures
Code

**Basics**

{- Make your own typeclass ex: class Eq a where ;;; Eq is the class, a is a type variable (==) :: a -> a -> Bool (/=) :: a -> a -> Bool x == y = not (x /= y) ;;; Define equality -> will overwrite below x /= y = not (x == y) ;;; Define inequality -} {- data Colors = Red | Green | Blue deriving (Eq) -} {- Overwrite only == then /= follows -} data Colors = Red | Green | Blue instance Eq Colors where Red == Red = True Green == Green = True Blue == Blue = True _ == _ = False {- Redefine == altogether -} data Colors1 = Turquoise | Orange | Yellow instance Eq Colors1 where Turquoise == Orange = True Orange == Yellow = True Yellow == Turquoise = True _ == _ = False instance Ord Colors1 where Turquoise < Yellow = True Orange < Turquoise = True _ < _ = False x > y = y < x instance Show Colors where show Red = "Red means stop" show Green = "Green means go" show Blue = "Blue does not belong" q1 = show [Red,Green,Blue] |
||