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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T21:30:30+00:00 2026-05-16T21:30:30+00:00

Take a quick peek at the following interactive session in GHCi: Prelude> import Control.Applicative

  • 0

Take a quick peek at the following interactive session in GHCi:

Prelude> import Control.Applicative
Prelude Control.Applicative> (+1) <$> [1,2]
[2,3]
Prelude Control.Applicative> (+1) <$> (1,2)
(1,3)

I guess there is a good reason for the behavior of <$> regarding pairs, but I wasn’t able to find one so far, so:

Why is <$> (or fmap) defined to act only on the second member of a pair and not on both values?

  • 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-16T21:30:31+00:00Added an answer on May 16, 2026 at 9:30 pm

    <$> (aka fmap) is a member of the Functor class like so:

    class Functor f where
      fmap :: (a -> b) -> f a -> f b
    

    So whatever f is must be a parameterised type with one type argument. Lists are one such type, when written in their prefix form [] ([] a is the same as [a]). So the instance for lists is:

    instance Functor [] where
      -- fmap :: (a -> b) -> [] a -> [] b 
      fmap = map
    

    Pairs can also be written in prefix form: (,) a b is the same as (a, b). So let’s consider what we do if we want a Functor instance involving pairs. We can’t declare an instance Functor (,) because the pair constructor (,) takes two types — and they can be different types! What we can do is declare an instance for (,) a — that’s a type that only needs one more type:

    instance Functor ( (,) a ) where
      -- fmap :: (b -> c) -> (,) a b -> (,) a c
      fmap f (x, y) = (x, f y)
    

    Hopefully you can see that the definition of fmap is the only sensible one we can give. The answer as to why the functor instance operates on the second item in a pair is that the type for the second item comes last in the list! We can’t easily declare a functor instance that operates on the first item in a pair. Incidentally, this generalises to larger tuples, e.g. the quadruple (,,,) a b c d (aka (a, b, c, d)) can also have a Functor instance on the last item:

    instance Functor ( (,,,) a b c) where
      -- fmap :: (d -> e) -> (,,,) a b c d -> (,,,) a b c e
      fmap f (p, q, r, s) = (p, q, r, f s)
    

    Hope that helps explain it all!

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

Sidebar

Related Questions

Take the following string as an example: The quick brown fox Right now the
Take the following C# class: c1 { event EventHandler someEvent; } If there are
Take the following generics example import java.util.List; import java.util.ArrayList; public class GenericsTest { private
I recently had to take a quick look at Adobe InDesign server. In this
I've got a quick question about default values in PL/SQL functions in Oracle. Take
Take the following snippet: List<int> distances = new List<int>(); Was the redundancy intended by
Take the following two lines of code: for (int i = 0; i <
Take this code: <?php if (isset($_POST['action']) && !empty($_POST['action'])) { $action = $_POST['action']; } if
Take a look at the ssl_requirement plugin. Shouldn't it check to see if you're
Take a .Net Winforms App.. mix in a flakey wireless network connection, stir with

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.