Re: How to print table data in a format and than query it

Neil Mitchell <[email protected]> Tue, 2 Jun 2009 11:50:10 +0100
Newsgroups gmane.comp.lang.haskell.hugs.user
Message-ID <[email protected]>
Hi

I think this email would be better suited to the haskell-cafe@ mailing
list, it's not really specific to Hugs, but is how to do these things
in Haskell.

Before you post there however I'd read the guidelines at:
http://www.haskell.org/haskellwiki/Homework_help

Thanks

Neil

On Tue, Jun 2, 2009 at 11:39 AM, napster007 <[email protected]> wrot=
e:
>
> This part is about embedding a simple query in Haskell. In this assignmen=
t
> you are specifically required to exploit Haskell's predefined higher-orde=
r
> 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 =3D Table
> =A0 =A0 =A0 =A0 =A0 =A0 String =A0 =A0 =A0 --table name
> =A0 =A0 =A0 =A0 =A0 =A0 [String] =A0 =A0 --field names
> =A0 =A0 =A0 =A0 =A0 =A0 [[String]] =A0 --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 giv=
en
> in square kilometers.
> top10 =3D Table
> =A0"LargestCountries"
> =A0[ "Rank","Country", "Area"]
> =A0[ [ "1", "Russia", "17,075,400"]
> =A0 =A0, [ "2", "Canada", "9,976,140"]
> =A0 =A0, [ "3", "United States", "9,629,091"]
> =A0 =A0, [ "4", "China", "9,596,960"]
> =A0 =A0, [ "5", "Brazil", "8,511,965"]
> =A0 =A0, [ "6", "Australia", "7,686,850"]
> =A0 =A0, [ "7", "India", "3,287,590"]
> =A0 =A0, [ "8", "Argentina", "2,776,890"]
> =A0 =A0, [ "9", "Kazakhstan", "2,717,306"]
> =A0 =A0, [ "10", "Sudan", "2,505,810"]
> =A0]
>
>
> The functionalities that you must implement are listed below:
> 1. =A0 =A0 =A0Show a table: write a function that formats a row, and then=
 maps this
> function over a table to format the table.
> 2. =A0 =A0 =A0Project a table: write a function that projects a row, and =
then maps this
> function over a table to project the table.
> 3. =A0 =A0 =A0Select from a table: write a function that determines wheth=
er 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 top=
10
> 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 ca=
n
> nicely print a table like below:
>
> LargestCountries:
> |Rank|Country =A0 =A0 =A0 =A0 =A0|Area =A0 =A0 =A0 =A0|
> -------------------------------------
> |1 =A0 |Russia =A0 =A0 =A0 =A0 =A0 |17,075,400 =A0|
> |2 =A0 |Canada =A0 =A0 =A0 =A0 =A0 |9,976,140 =A0 |
> |3 =A0 |United States =A0 =A0|9,639,091 =A0 |
> |4 =A0 |China =A0 =A0 =A0 =A0 =A0 =A0|9,596,960 =A0 |
> |5 =A0 |Brazil =A0 =A0 =A0 =A0 =A0 |8,511,965 =A0 |
> |6 =A0 |Australia =A0 =A0 =A0 =A0|7,686,850 =A0 |
> |7 =A0 |India =A0 =A0 =A0 =A0 =A0 =A0|3,287,590 =A0 |
> |8 =A0 |Argentina =A0 =A0 =A0 =A0|2,776,890 =A0 |
> |9 =A0 |Kazakhstan =A0 =A0 =A0 |2,717,306 =A0 |
> |10 =A0|Sudan =A0 =A0 =A0 =A0 =A0 =A0|2,505,810 =A0 |
>
> 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=3DputStr.writeTable
>
> Write your solution modularly. Small sub-functionalities which are used
> repeatedly are best implemented as separate functions, so you can reuse t=
hem
> 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 proje=
ct
> with the following type:
> project::[String]->Table->Table
> The application project fields table returns a new table consisting of on=
ly
> the columns from the original table whose names is specified in fields. S=
o,
> for example,
> printTable.project["Rank", "Country"]$top10
> will produce the following table:
>
> LargestCountries:
> |Rank|Country =A0 =A0 =A0 =A0 =A0|
> ------------------------
> |1 =A0 |Russia =A0 =A0 =A0 =A0 =A0 |
> |2 =A0 |Canada =A0 =A0 =A0 =A0 =A0 |
> |3 =A0 |United States =A0 =A0|
> |4 =A0 |China =A0 =A0 =A0 =A0 =A0 =A0|
> |5 =A0 |Brazil =A0 =A0 =A0 =A0 =A0 |
> |6 =A0 |Australia =A0 =A0 =A0 =A0|
> |7 =A0 |India =A0 =A0 =A0 =A0 =A0 =A0|
> |8 =A0 |Argentina =A0 =A0 =A0 =A0|
> |9 =A0 |Kazakhstan =A0 =A0 =A0 |
> |10 =A0|Sudan =A0 =A0 =A0 =A0 =A0 =A0|
>
> 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 colum=
n
> field satisfies the predicate p.
> Here is an example:
> printTable.select "Rank" p $ top10 where p x =3D read x < (6::Int)
> will produce the following table:
>
> LargestCountries:
> |Rank|Country =A0 =A0 =A0 =A0 =A0|Area =A0 =A0 =A0 =A0|
> -------------------------------------
> |1 =A0 |Russia =A0 =A0 =A0 =A0 =A0 |17,075,400 =A0|
> |2 =A0 |Canada =A0 =A0 =A0 =A0 =A0 |9,976,140 =A0 |
> |3 =A0 |United States =A0 =A0|9,639,091 =A0 |
> |4 =A0 |China =A0 =A0 =A0 =A0 =A0 =A0|9,596,960 =A0 |
> |5 =A0 |Brazil =A0 =A0 =A0 =A0 =A0 |8,511,965 =A0 |
> Implement the function select.
>
>
> --
> View this message in context: http://www.nabble.com/How-to-print-table-da=
ta-in-a-format-and-than-query-it-tp23829287p23829287.html
> Sent from the Haskell - Hugs-Users mailing list archive at Nabble.com.
>
> _______________________________________________
> Hugs-Users mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/hugs-users
>