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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:08:06+00:00 2026-05-26T23:08:06+00:00

Im trying to understand what a functor is, i found this tutorial/example: http://en.wikibooks.org/wiki/Haskell/Solutions/Applicative_Functors data

  • 0

Im trying to understand what a functor is, i found this tutorial/example:

http://en.wikibooks.org/wiki/Haskell/Solutions/Applicative_Functors

 data Tree a = Node a [Tree a]

The functor for the above type being:

 instance Functor Tree where
   fmap f (Node a ts) = Node (f a) (map (fmap f) ts)

could someone help explain what exactly they have done and why they have done it? My understanding is that a functor allows you to iterate over a data type. I cant seem to understand the syntax used though?

  • 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-26T23:08:07+00:00Added an answer on May 26, 2026 at 11:08 pm

    A Functor is useful for mapping between two data representations. Sometimes that might resemble iteration, sometimes not. Having this common Functor typeclass allows us to ignore the actual structure of the data type (Maybe, List, Tree) and focus only on the data they contain. The author of that data type should know how that data structure might be traversed/iterated so he should provide the implementation for that (in the form of a Functor instance of that data type). All we have to provide is that function f which takes an a and maps it to a b. For example:

    import Data.Char (toLower)
    
    data Tree a = Node a [Tree a]
      deriving Show
    
    instance Functor Tree where
      fmap f (Node a ts) = Node (f a) (map (fmap f) ts)
    
    main :: IO ()
    main = do print (toLower `fmap` (Node 'F' [])) -- Node 'f' []
              print (toLower `fmap` (Just 'F'))    -- Just 'f'
              print (toLower `fmap` "FOO")         -- "foo"
    

    We were able to lowercase those chars using the same code toLower combined with fmap.

    So, what you should do when defining that Functor instance is to extract inner data using pattern matching, and the apply the received callback function f to each of these results.

    An infix synonym for fmap can be found in the Control.Applicative module, called <$>.

    main :: IO ()
    main = do print (toLower <$> (Node 'F' [])) -- Node 'f' []
              print (toLower <$> (Just 'F'))    -- Just 'f'
              print (toLower <$> "FOO")         -- "foo"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Trying to understand Ruby a bit better, I ran into this code surfing the
trying to understand http and headers i was playing around with telnet to send
Trying to understand why this doesn't work. I keep getting the following errors: left
Trying to understand this binding process of the WPF. See the code at the
In trying to understand the correlation between program objects and data in tables (here:
Trying to understand how CoffeeScript instance and class variable works I came with this
Trying to understand this open source app on github, it has a gem file:
Im trying to understand when to call autorelease, and what this will actually do
Trying to understand how you're supposed to read files in python. This is what
While trying to understand the MVVM pattern I came across this video linked from

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.