Dianne
Dianne

Reputation: 47

Query evaluation in class show

I'm working on a project and I need a little help, please!

type CSV = String
type Table = [[String]]

table_csv :: Table -> CSV
csv_table :: CSV -> Table

data query = CSV CSV | Table Table | List [String]

How can I enroll "query" in class Show? For Table, use table_csv, and for List and String, use default.

instance Show query where
    ...

I know that first CSV refers to constructor name, and second to type CSV.

table_csv and csv_table are already implemented by me. Thanks a lot!

Upvotes: 1

Views: 37

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476729

Types have names that start with an uppercase, so you should implement this as:

data Query = CSV CSV | Table Table | List [String]

We can implement an instance of Show by using pattern matching and thus use the correct items:

instance Show Query where
    show (CSV csv) = csv
    show (Table tab) = table_csv tab
    show (List items) = "List " <> show items

For the List … you can of course implement this in a different way.

Upvotes: 2

Related Questions