Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8222613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:20:19+00:00 2026-06-07T14:20:19+00:00

I wrote a mathematical Vector module in Haskell. So I started out with: data

  • 0

I wrote a mathematical Vector module in Haskell.

So I started out with:

data Vector a = Vector !a !a !a deriving (Eq, Show)

Fine—this lets me use any numerical data type I want. The problem is that I don’t want to be writing Double and Vector Double everywhere for the simple reason that I shouldn’t have to. So I add:

type Scalar = Double
type Vector = Vector Scalar

But of course that second line is wrong, since there are now two declarations of Vector. So what should I change it to? I think to myself, no, I’m going to be writing this all over my code, so I want to leave the type alias simply as Vector. Which means I have to change the data type name. But if I change that, then I feel like I should also change the constructor, which makes everything more confusing. But if it feels awkward making the constructor have the same name as the type alias this way.

Right now I have this:

type Scalar = Double
type Vector = VectorT Scalar

data VectorT a = Vector !a !a !a deriving (Eq, Show)

I picked T arbitrarily (I guess it stands for “type”) but I’m not so sure about this. Normally when I document functions I would say -- Calculate the magnitude of a Vector, but with VectorT I feel like I should really be using that type name. So I resort to just referring to them as vectors (not capitalized)—except then I feel like I have to apply this convention to every comment for every data type.

Has anyone been in a similar situation? Can anyone think of a more elegant solution in this case?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:20:22+00:00Added an answer on June 7, 2026 at 2:20 pm

    One solution to your particular problem would be to have the data type in a different module from the type synonym. That is, have a Math.Vector module which contains the data declaration and some generic functions (that is, functions working for all numeric types). Then, when you actually use Vector Double in your code a lot, just create the type synonym using a qualified import:

    import qualified Math.Vector as MV
    
    type Scalar = Double
    type Vector = MV.Vector Scalar
    

    I think this makes sense from a code organization standpoint. Particularly, if you’ve defined your Vector type to work on all numeric types, I expect the functions in that module to also work on all numeric types. The fact that you use Vector Double a lot in some other part of the code should not affect the module where Vector is actually defined. After all, it’s completely reasonable to imagine using Vector Int a lot in yet another part of your program.

    As an aside, I’m not sure calling it Vector is the best idea. A vector does not necessarily have to have three dimensions, so I would call your data type something like Vector3D. This is actually the name used in some other APIs (like the Java 3D API), so it’s probably a good choice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote this post Music and Mathematics, finding the Natural and the Pentatonic scales.
I wrote a symfony task to fill a database of sample data. Here's a
Let's say you want to do some simple vector calculation: //this could be C++
I have a mathematical formula to apply: I wrote a non recursive code (sadly
I'm trying to write a small Python module which contain some mathematical functions. For
I'm having a problem using a mathematical operator in a switch expression. This is
I wrote a parser for a non-trivial language this weekend. Some of the output
this is mathematical question for programming needs... whats the way to calculate probability if
Leonid wrote in chapter iv of his book : "... Module, Block and With.
I have started a bounty for this question ...because I really want the community's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.