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

  • Home
  • SEARCH
  • 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 7414469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:06:18+00:00 2026-05-29T07:06:18+00:00

I can create and reference relative pointers to struct members in C++ using the

  • 0

I can create and reference relative pointers to struct members in C++ using the ::*, .*, and ->* syntax like :

char* fstab_t::*field = &fstab_t::fs_vfstype;
my_fstab.*field = ...

In Haskell, I can easily create temporary labels for record getters like :

(idxF_s,idxL_s) = swap_by_sign sgn (idxF,idxL) ;

Afaik, I cannot however then update records using these getters as labels like :

a { idxF_s = idxL_s b }

Is there an easy way to do this without coding for each record setter?

  • 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-29T07:06:19+00:00Added an answer on May 29, 2026 at 7:06 am

    A getter and setter bundled together in a first-class value is referred to as a lens. There are quite a few packages for doing this; the most popular are data-lens and fclabels. This previous SO question is a good introduction.

    Both of those libraries support deriving lenses from record definitions using Template Haskell (with data-lens, it’s provided as an additional package for portability). Your example would be expressed as (using data-lens syntax):

    setL idxF_s (b ^. idL_s) a
    

    (or equivalently: idxF_s ^= (b ^. idL_s) $ a)

    You can, of course, transform lenses in a generic way by transforming their getter and setter together:

    -- I don't know what swap_by_sign is supposed to do.
    negateLens :: (Num b) => Lens a b -> Lens a b
    negateLens l = lens get set
      where
        get = negate . getL l
        set = setL l . negate
    

    (or equivalently: negateLens l = iso negate negate . l1)

    In general, I would recommend using lenses whenever you have to deal with any kind of non-trivial record handling; not only do they vastly simplify pure transformation of records, but both packages contain convenience functions for accessing and modifying a state monad’s state using lenses, which is incredibly useful. (For data-lens, you’ll want to use the data-lens-fd package to use these convenience functions in any MonadState; again, they’re in a separate package for portability.)


    1 When using either package, you should start your modules with:

    import Prelude hiding (id, (.))
    import Control.Category
    

    This is because they use generalised forms of the Prelude’s id and (.) functions — id can be used as the lens from any value to itself (not all that useful, admittedly), and (.) is used to compose lenses (e.g. getL (fieldA . fieldB) a is the same as getL fieldA . getL fieldB $ a). The shorter negateLens definition uses this.

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

Sidebar

Related Questions

I can create the following and reference it using area[0].states[0] area[0].cities[0] var area =
How can I create an instance of an object using a class reference, and
How do I create a resource that I can reference and use in various
The goal: To create a .NET dll i can reference from inside SQL Server
I know you can create 2 object reference variables, such as: Book b; Book
How can I reference a form by name using JQuery? I have 3 different
In LINQ to SQL, I can create a repository dynamically using DataContext.GetTable<T> . Is
I know I can create an array and a reference to an array as
I'm using Prism so I can create a modular solution. I need to access
You can create a class reference with the following code: Class M = [NSMutableString

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.