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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:29:19+00:00 2026-05-24T04:29:19+00:00

Suppose I have a Vector datatype defined as follows: data Vector = Vector {

  • 0

Suppose I have a Vector datatype defined as follows:

data Vector = Vector { x :: Double
                     , y :: Double
                     , z :: Double
                     }

Would it be more usual to define functions against it using member access:

vecAddA v w
    = Vector (x v + x w)
             (y v + y w)
             (z v + z w)

Or using pattern-matching:

vecAddB (Vector vx vy vz) (Vector wx wy wz)
    = Vector (vx + wx)
             (vy + wy)
             (vz + wz)

(Apologies if I’ve got any of the terminology incorrect).

  • 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-05-24T04:29:20+00:00Added an answer on May 24, 2026 at 4:29 am

    I would normally use pattern matching, especially since you’re using all of the constructor’s arguments and there aren’t a lot of them. Also, In this example it’s not an issue, but consider the following:

    data Foo = A {a :: Int} | B {b :: String}
    
    fun x = a x + 1
    

    If you use pattern matching to do work on the Foo type, you’re safe; it’s not possible to access a member that doesn’t exist. If you use accessor functions on the other hand, some operations such as calling fun (B "hi!") here will result in a runtime error.

    EDIT: while it’s of course quite possible to forget to match on some constructor, pattern matching makes it pretty explicit that what happens depends on what constructor is used (you can also tell the compiler to detect and warn you about incomplete patterns) whereas the use of a function hints more that any constructor goes, IMO.

    Accessors are best saved for cases when you want to get at just one or a few of the constructor’s (potentially many) arguments and you know that it’s safe to use them (no risk of using an accessor on the wrong constructor, as in the example.)

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

Sidebar

Related Questions

Suppose i have a std::vector<int> v //and ... for(int i =0;i<100;++i) v.push_back(i); now i
Suppose I have two containers of elements: std::vector<std::string> foo = { aa, bb, cc
Suppose I have a class: public class Vector { public float X { get;
Suppose I have a class like this: class MyClass { private: vector<MyOtherClass> myMember; public:
Suppose I have some number a , and I want to get vector [
Suppose I have a list with observations: foo <- list(c(C, E, A, F), c(B,
Suppose I have an object with many members: class Example { AnotherClass member1; AnotherClass
I have encountered a bit of a hiccup in something I'm working on. Suppose
I am using a 3rd-party rotator object, which is providing a smooth, random rotation
I'm attempting the Write Yourself a Scheme in 48 Hours tutorial and as someone

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.