[Hugs-users] How to print table data in a format and than query it

napster007 life_vs_time at hotmail.com
Tue Jun 2 06:39:01 EDT 2009


This part is about embedding a simple query in Haskell. In this assignment
you are specifically required to exploit Haskell's predefined higher-order
functions. 
You can use the following Higher Order Functions: 
map, filter, zipWith, any, all, takeWhile, dropWhile 
You can represent a database table with a value of this type: 
data Table = Table
             String       --table name
             [String]     --field names
             [[String]]   --records
Suppose now we have a value Table n s z. It represents a table whose name is
n; s specifies the names of the columns in the table; and z is a list of the
records in the table. 
To keep it simple, you can represent all those values using plain string 
Below is the table represented in that way. The table contains data about
the top-10 Largest Countries in the world with its corresponding area given
in square kilometers.
top10 = Table
  "LargestCountries"
  [ "Rank","Country", "Area"]
  [ [ "1", "Russia", "17,075,400"]
    , [ "2", "Canada", "9,976,140"]
    , [ "3", "United States", "9,629,091"]
    , [ "4", "China", "9,596,960"]
    , [ "5", "Brazil", "8,511,965"]
    , [ "6", "Australia", "7,686,850"]
    , [ "7", "India", "3,287,590"]
    , [ "8", "Argentina", "2,776,890"]
    , [ "9", "Kazakhstan", "2,717,306"]
    , [ "10", "Sudan", "2,505,810"]
  ]


The functionalities that you must implement are listed below: 
1.	Show a table: write a function that formats a row, and then maps this
function over a table to format the table. 
2.	Project a table: write a function that projects a row, and then maps this
function over a table to project the table. 
3.	Select from a table: write a function that determines whether a row
satisfies the given selection criteria, then filter the table using this
function.
Functionality 1: Show a Table
You cannot unfortunately view a table (yet), for example, try to type top10
in hugs. Hugs will complain that it does not know how to show a table. So,
this is your first task. Write a function printTable::Table->IO() that can
nicely print a table like below: 

LargestCountries:
|Rank|Country          |Area        |
-------------------------------------
|1   |Russia           |17,075,400  |
|2   |Canada           |9,976,140   |
|3   |United States    |9,639,091   |
|4   |China            |9,596,960   |
|5   |Brazil           |8,511,965   |
|6   |Australia        |7,686,850   |
|7   |India            |3,287,590   |
|8   |Argentina        |2,776,890   |
|9   |Kazakhstan       |2,717,306   |
|10  |Sudan            |2,505,810   |

It will be easier if you first write: 
writeTable::Table->String
which simply converts a table to a flat string, and then printTable is
simply: 
printTable=putStr.writeTable

Write your solution modularly. Small sub-functionalities which are used
repeatedly are best implemented as separate functions, so you can reuse them
in multiple places. 
Functionality 2: Select Columns
The next operation you must implement is so-called projection. That is,
getting a selected set of columns from a table. Consider a function project
with the following type: 
project::[String]->Table->Table 
The application project fields table returns a new table consisting of only
the columns from the original table whose names is specified in fields. So,
for example, 
printTable.project["Rank", "Country"]$top10
will produce the following table: 

LargestCountries:
|Rank|Country          |
------------------------
|1   |Russia           |
|2   |Canada           |
|3   |United States    |
|4   |China            |
|5   |Brazil           |
|6   |Australia        |
|7   |India            |
|8   |Argentina        |
|9   |Kazakhstan       |
|10  |Sudan            |

Your task is to implement this function project. 
Functionality 3: Select Records
The next operation is used to select rows (records) from a database.
Consider a function select with the following type: 
select::String->(String->Bool)->Table->Table
The application select field p table will return a new table, containing
only the rows r from the original table such that the value of r at column
field satisfies the predicate p. 
Here is an example: 
printTable.select "Rank" p $ top10 where p x = read x < (6::Int)
will produce the following table: 

LargestCountries:
|Rank|Country          |Area        |
-------------------------------------
|1   |Russia           |17,075,400  |
|2   |Canada           |9,976,140   |
|3   |United States    |9,639,091   |
|4   |China            |9,596,960   |
|5   |Brazil           |8,511,965   |
Implement the function select. 


-- 
View this message in context: http://www.nabble.com/How-to-print-table-data-in-a-format-and-than-query-it-tp23829287p23829287.html
Sent from the Haskell - Hugs-Users mailing list archive at Nabble.com.



More information about the Hugs-Users mailing list