I cannot understand what is this “instance” means in Haskell (in line 19) :
16 > type Prog = [Function]
17 > data Function = Defun String String Exp
18 > -- deriving Show
19 > instance Show Function where
20 > show (Defun n p e) = "\n" ++ n ++ "(" ++ p ++ ") { return " ++ show e ++ "; }\n"
21 > showList [] = showString ""
22 > showList (f:fs) = shows f . showl fs
23 > where showl [] = showString ""
24 > showl (f:fs) = shows f . showl fs
thx.
Read about type classes.
Showis a type class and you are defining aShowinstance forFunctiondatatype, so you can do something likeand it will convert this to a string representation using the show definition for
Function.If you are from OO background then you can think like overloading the
showfunction forFunctiondata type.