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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:40:47+00:00 2026-06-02T15:40:47+00:00

Given an example data type with record syntax: data VmInfo = VmInfo {infoVid ::

  • 0

Given an example data type with record syntax:

data VmInfo = VmInfo {infoVid   :: String
                     ,infoIndex :: Int
                     ,infoPid   :: Int
                     ,infoExe   :: String
                     } deriving (Show)

and (vmInfo :: String -> VmInfo) function that generates and returns the above data structure given vm name as string.

I can see two methods to extract the individual parts of the VmInfo data type.

(VmInfo vid _ _ _) <- vmInfo vm

Which is just a pattern match. And …

vid <- infoVid <$> vmInfo vm

using record syntax compiler generated functions.

The question is simple: which is a preferred method?

Amount-of-typing wise they are the same so I am looking for speed and correctness/best practice.

I assume the pattern matching would be faster but then what is the point of record syntax?

  • 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-02T15:40:50+00:00Added an answer on June 2, 2026 at 3:40 pm

    These aren’t semantically equivalent.

    Let’s look at the first example:

    (VmInfo vid _ _ _) <- vmInfo vm
    

    This performs a pattern match in the binding operation. There are two results of this. The first is that the constructor of the result of the vmInfo vm action is evaluated. This means that if vmInfo ended with a line like return undefined, the exception thrown by evaluating undefined would happen at this pattern match, not a later use of vid. The second is that if the pattern match is refuted (the pattern match does not match the value), the monad’s fail instance will be called with the pattern match error text. That’s not possible in this case, but it is generally possible when pattern matching a constructor in a bind.

    Now, on to the next example:

    vid <- infoVid <$> vmInfo vm
    

    By the definition of <$>, this will be entirely lazy in the value returned by the action (not the effects). If vmInfo ended with return undefined, you wouldn’t get the exception from evaluating undefined until you did something that used the value of vid. Additionally, if infoVoid had the ability to throw any exceptions, they wouldn’t end up happening until the use of vid, best case.

    Interestingly enough, these differences are only present in the scope of a monadic bind. If vmInfo was pure and you were binding the name vid inside a let or where expression, they would generate identical code.

    In that case, which one you would want to use is entirely up to you. Both are idiomatic Haskell. People generally pick whichever looks better in the context they’re working in.

    The main reasons people use accessor functions is brevity when the record has so many fields a pattern match is huge, and because they are actual functions – they can be passed to any higher-order function their type fits into. You can’t pass around pattern matches as a distinct construct.

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

Sidebar

Related Questions

Given the following example data: Users +--------------------------------------------------+ | ID | First Name | Last
How can you sort the following word by the given rule? Example data is
In the following contrived example, data is given a warning due to it being
Given the example below, can someone please show me how this could be called?
This is pretty simple but I'm at a loss: Given this type of data
Can you give an example where the Queue data structure can be specially helpful
This example uses a StringWriter to hold the serialized data, then calling ToString() gives
when I look up the syntax for .data(), it gives examples like this: $('body').data('foo',
When using downloaded R packages, such as tm, the given example usually loads an
I'm trying to solve this problem : http://uva.onlinejudge.org/external/7/732.html . For the given example, they

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.