[Haskell-beginners] How to "add column" to a Table?

martin martin.drautzburg at web.de
Mon Apr 27 06:46:56 UTC 2015


Am 04/26/2015 um 09:46 PM schrieb Michael Orlitzky:
> On 04/26/2015 06:13 AM, martin wrote:
>> Hello all,
>>
>> I suppose the natural representation for a Table is a List of Tuples. Or more generally I'll probably end up with a type
>> "Table a" where a stands for the Columns.
>>
>> I cannot figure out how to add a new Column to a Table. What would be the type of such an operation? If I just want to
>> add an empty Column (which what an RDBMS would do), would I do something like this?
>>
>> addColumn :: ColumnDef -> Table a -> Table b
>>
>> But what is this ColumnDef? It would need to contain the name and type of the new column. To implement this operation I
>> would have to somewehre add a column to something. But what is the type of this something. I don't see how I can add a
>> component to a Tuple in a generic way, or can I?
>>
> 
> type Person = ( Int,    -- ID
>                 String, -- Name
>                 Int,    -- Age
>                 Bool    -- Evil bit
>               )
> 
> type Place = ( Int,    -- ID
>                String, -- Name
>                Double, -- Longitude
>                Double, -- Latitude
>                Double  -- Elevation
>              )
> 
> data Column = Person | Place
> 
> type Table = [Column]
> 
> addColumn :: Table -> Column -> Table
> addColumn table col = table ++ [col]

Aren't you adding a *row* to a Table which allows rows of multiple shapes? What I am looking for is an operation which
adds a *column* to a regular table, i.e. one where all rows have the same shape.

But I like your idea of adding an "Evil bit" to Persons.



More information about the Beginners mailing list