[Haskell-beginners] Suggestions for creating a "bind" function that can be applied to different things?

Costello, Roger L. costello at mitre.org
Tue Aug 2 01:44:38 CEST 2011


Hi Folks,

When I say:
 
    "Let x be of datatype Int."

 I think the proper terminology is "bind". That is, I bind the name "x" to the Int datatype.

Is that the correct terminology?

Assuming it is, I would like to bind a name to many different things. I seek your suggestions on how to do this.

First let dt1, dt2, dt3, dt4 be "DataTypes":

    dt1, dt2, dt3, dt4 :: DataType

And let "Name" be synonymous with String:

    type Name = String

And let title, author, date, cost be "Elements":

    title, author, date, cost, book :: Element

I would like a "bind" function that binds a Name to a DataType to create an Element:

    bind    ::    Name -> DataType -> Element

Here are a few examples of using bind to create Elements:

    title       =   bind "Title" dt4

    author   =   bind "Author" dt4

    date      =   bind "Date" dt4

I would also like to bind a Name to a DataType to create an "Attribute":

    bind :: Name -> DataType -> Attribute

Here's an example of using bind to create an Attribute:

    currency   =   bind "Currency" dt3

And I'd like to bind an Attribute to an Element:

     bind   ::   Attribute -> Element -> Element

Here's an example of binding the currency Attribute to a cost Element:

    cost   =   bind currency (bind "Cost" dt1)

Finally, I'd like to create an Element by binding a Name to a list of Elements:

    bind   ::   Name -> [Element] -> Element

Here's an example of this:

    book   =   bind "Book" [title, author, date, cost]

As you see, I'd like to "bind" to lots of different things.

But I can't create multiple functions with the same name, "bind".

And I'd rather not create a function for each thing that I want to bind to, e.g., elementBind, attributeBind, attributeToElementBind, multipleElementsBind.

There must be an elegant solution. 

What is your suggestion?

/Roger



More information about the Beginners mailing list