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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:46:57+00:00 2026-05-30T15:46:57+00:00

Lately I’ve been writing FFI code that returns a data structure in the IO

  • 0

Lately I’ve been writing FFI code that returns a data structure in the IO monad. For example:

peek p = Vec3 <$> (#peek aiVector3D, x) p
              <*> (#peek aiVector3D, y) p
              <*> (#peek aiVector3D, z) p

Now I can think of four nice ways to write that code, all closely related:

peek p = Vec3 <$> io1 <*> io2 <*> io3
peek p = liftA3 Vec3 io1 io2 io3
peek p = return Vec3 `ap` io1 `ap` io2 `ap` io3
peek p = liftM3 Vec3 io1 io2 io3

Notice that I’m asking about monadic code that doesn’t require anything beyond what Applicative provides. What is the preferred way to write this code? Should I use Applicative to emphasize what the code does, or should I use Monad because it might (?) have optimizations over Applicative?

The question is slightly complicated by the fact that there are only [liftA..liftA3] and [liftM..liftM5] but I have several records with more than three or five members, so if I decide to go with lift{A,M} I lose some consistency because I would have to use a different method for the larger records.

  • 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-30T15:46:58+00:00Added an answer on May 30, 2026 at 3:46 pm

    The first thing to remember is that this is slightly more complicated than it ought to be–any Monad instance should have an associated Applicative instance such that the liftM and liftA functions coincide. As such, here’s two guidelines:

    • If you’re writing a generic function for any Monad, use liftM &co. to avoid incompatibility with other functions that have only a Monad constraint.

    • If you’re working with a specific Monad instance that you know has an accompanying Applicative instance, use Applicative operators consistently for any definition or subexpression where you don’t need Monad operations, but avoid mixing them aimlessly.

    Should I use Applicative to emphasize what the code does, or should I use Monad because it might (?) have optimizations over Applicative?

    In general, if there is a difference, it will be the other way around. Applicative only supports a static “structure” of the computation, whereas Monad permits embedded control flow. Consider lists, for instance–with Applicative, all you can do is generate all possible combinations and transform each one–the number of result elements is determined entirely by the number of elements in each input. With Monad, you can generate different numbers of elements at each step based on input elements, allowing you to filter or expand arbitrarily.

    A more potent example is is the Applicative and Monad instances based on zipping infinite streams–Applicative can simply zip them together in the obvious way, whereas Monad has to recalculate lots of stuff that it then throws away.

    So, the final issue is of liftA2 f x y vs. f <$> x <*> y, or the Monad equivalents. My advice here would be the following guidelines:

    • If you’re writing every argument out anyway, use the infix form, because it’s easier to read for large expressions.
    • If you’re just lifting an existing function, use foo = liftA2 bar rather than foo x y = bar <$> x <*> y–it’s shorter and more clearly expresses what you’re doing.

    And finally, on the issue of consistency, there’s no reason you couldn’t simply define your own liftA4 and so on, if you need them.

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

Sidebar

Related Questions

Lately I've been writing some JS code using jQuery and JavaScript as it is
Lately, I've been reading some code that has if (! (a == b) )
Lately I've been wondering if there's a difference between initializing the variables that have
Lately, I've come across a lot of Java code that relies on properties files
lately I have been writing a lot of tiny plugins for my web projects
Lately I have been finding that some of the ColdFusion applications on my production
Lately, i have been exploring the source code of OutputCache attribute in asp.net mvc-3
Lately, I've gotten some weird linker errors. I've been taught that there's two ways
Lately we've been having issues on entity framework saving data objects even though we
So lately I have been trying ways to pass data from a child to

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.