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 7982413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:40:25+00:00 2026-06-04T10:40:25+00:00

I want a function +++ that adds two mathematical vectors. I could implement vectors

  • 0

I want a function +++ that adds two mathematical vectors.

I could implement vectors as [x, y, z] and use:

(+++) :: (Num a) => [a] -> [a] -> [a]
(+++) = zipWith (+)

And thus accomodate any n-dimensional vector (so this would work for [x, y] too).

Or I could implement vectors as (x, y, z) and use:

type Triple a = (a, a, a)

merge :: (a -> b -> c) -> Triple a -> Triple b -> Triple c
merge f (a, b, c) (x, y, z) = (f a x, f b y, f c z)

(+++) :: (Num a) => Triple a -> Triple a -> Triple a
(+++) = merge (+)

Of course this is slightly more complex but it when I implement all the other vector functions, that is irrelevant (50 lines instead of 40).

The problem with the list approach is that I can add a 2D vector with a 3D vector. In that case, zipWith would simply chop off the 3D vector’s z component. While that might make sense (more likely it should expand the 2D vector to [x, y, 0]), for other functions I’m thinking it could be problematic to have either happen silently. The problem with the tuple approach is it limits the vector to 3 components.

Intuitively, I would think that it would make more sense to represent vectors as (x, y, z), since a mathematical vector has a fixed number of components and it doesn’t really make sense to cons (prepend) a component to a vector.

On the other hand, although it’s very unlikely that I will need anything other than 3D vectors, it doesn’t seem quite right to limit it to that.

I guess what I want is functions that take two lists of equal length, or better, functions that operate on tuples of arbitrary size.

Any suggestions, in terms of practicality, scalability, elegance, etc.?

  • 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-04T10:40:27+00:00Added an answer on June 4, 2026 at 10:40 am

    Landei’s and leftaroundabout’s answers are good (thanks to you both), and I guess I should have realized that this wouldn’t be as simple as I’d hoped. Trying to do either of the options I suggested makes for complex code, which woudn’t be a problem in itself except that it seems the user code wouldn’t be very pretty to look at either.

    I think I’ve decided to go with tuples and stick with 3-dimension only vectors, simply because it seems more semantically correct than using lists. I’m ending up re-implenting map, zipWith, sum and others for triples, though. I want to stick with simplicity—I feel as though if I had a compelling argument to think of vectors as lists, then that solution would work better (provided I make sure I don’t mix dimensions)… When I actually use the vectors, though, functions will take a 3d vector as an argument, not one of variable dimensions, and Num a => [a] can’t enforce that.

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

Sidebar

Related Questions

Say I want a function that takes two floats ( x and y ),
I'm trying to use a function that adds a span around the first word
I just want a function that can take 2 parameters: the URL to POST
I'm trying to call a function that contains jQuery code. I want this function
I want a python function that takes a pdf and returns a list of
I want to get lambda function that will query for items which were submitted
i want to write a function that prints multi-dimensional objects which are text (or
I want to write 'twice' function that takes a function and an argument and
I want to make a function that is always running in xcode similar to
I want to write a function that read line by line from a socket

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.