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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:06:09+00:00 2026-05-14T19:06:09+00:00

foldr:: (a -> b -> b) -> b -> [a] -> b map ::

  • 0
foldr:: (a -> b -> b) -> b -> [a] -> b
map :: (a -> b) -> [a] -> [b]
mys :: a -> a
(.) :: (a -> b) -> (c -> a) -> c -> b

what is inferred type of:
a.map mys ::
b.mys map ::
c.foldr map ::
d.foldr map.mys ::

I’ve tried to create mys myself using mys n = n + 2 but the type of that is

mys :: Num a => a -> a

What’s the difference between ‘Num a => a -> a’ and just ‘a -> a’? Or what does ‘Num a =>’ mean? Is it that mys would only take Num type?

So anyways,
a) I got [a] -> [a] I think because it would just take a list and return it +2’d according to my definition of mys
b) (a -> b) -> [a] -> [b] I think because map still needs take both arguments like (*3) and a list then returns [a] which goes to mys and returns [b]
c) I’m not sure how to do this 1.
d) I’m not sure how to do this 1 either but map.mys means do mys first then map right?

Are my answers and thoughts correct? If not, why not?

THANKS!

  • 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-14T19:06:10+00:00Added an answer on May 14, 2026 at 7:06 pm

    (Update: Apparently my original answer wasn’t very helpful for the OP… See below the second HR for an appendix.)


    In situations like this, what you really want to do is to launch ghci and use :t to find out the type of various expressions. E.g.

    :t foldr map
    -- answer: foldr map :: [b] -> [b -> b] -> [b]
    

    If you need to define a name first, use let (which would not be needed in a Haskell source file):

    let mys = undefined :: a -> a
    :t map mys
    -- answer: [a] -> [a]
    

    Note the use of undefined with an explicit type signature. It’s perfectly ok for finding out the type of expressions of various forms and might even be useful in actual code as a placeholder at early planning stages.

    The Num a => is a type class constraint on a; see e.g. Type Classes and Overloading from “A Gentle Introduction to Haskell, Version 98” or Chapter 6. Using Typeclasses from “Real World Haskell” for more information. (Basically, it does what you think it does. :-))


    The above should be useful for verifying your answers (and the resources on type classes are good). As for how to solve problems of this kind yourself:

    Type inference is an application of what is called “the unification algorithm”. Google for “unification” for a number of resources; if you add a name of a programming language to your query, you’ll likely find example implementations.

    As for how to approach the examples at hand…

    a. map mys: map takes a function of type a -> b and returns a function of type [a] -> [b]. In general, a can be different to b, but mys is of type a -> a, so the returned function will be of type [a] -> [a].

    Here’s some hand-unification:

    (a -> b) -> [a] -> [b]`  
    (a -> a)`
          ^-- at this point we unify a with b;
              when propagated to the return type,
              this produces [a] -> [a]
    

    b. mys map: mys is a function which takes an object of some type and returns an object of the same type. In particular, if you pass it an argument of type (a -> b) -> [a] -> [b], that will be the type of the return value.

    Incidentally, there is only one “interesting” (not undefined) function whose type signature is a -> a (without type class constraints), namely id. See Philip Wadler’s paper “Theorems for free!” (which you can download from this page) for an extended discussion.

    c. foldr map: Firstly, note that the as and bs in foldr‘s signature having nothing to do with those in map‘s signature. It might be convenient to rename map‘s a to c and b to d and use the signature map :: (c -> d) -> [c] -> [d] to make this more readily apparent below. Also note that (a -> b -> b) is just a simpler way of writing (a -> (b -> b)).

    More hand-unification (explanation follows below):

    (a       -> (b  -> b))
    (c -> d) -> [c] -> [d]
    

    What happens here is that foldr takes a function argument of type (a -> (b -> b)). If you pass in map as that argument, the a gets unified with c -> d, the b with [c] and then again with [d], which means c equals d.

    The return value of foldr has type b -> [a] -> b; substituting the more specific types obtained in the previous paragraph we get

    [c] -> [c -> c] -> [c]
                 ^-- c equals d, right?
    

    The c comes from our changed signature for map; with the original b, this becomes

    [b] -> [b -> b] -> [b]
    

    d. foldr map . mys: This is actually (foldr map) . mys, not foldr (map . mys) — function application (“the invisible operator”) binds the strongest! Combining a. and c. from above to solve this one is left as an exercise to the reader. 😉

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is a nifty tool which generates some static html… May 16, 2026 at 12:44 pm
  • Editorial Team
    Editorial Team added an answer Since keysOfEntriesPassingTest returns a set of values, it would be… May 16, 2026 at 12:44 pm
  • Editorial Team
    Editorial Team added an answer You are using ImageMagick 6.2.8; the latest is 6.6.3. There… May 16, 2026 at 12:44 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I see that I can map a function over mutable arrays with mapArray, but
I'm kind of new in Haskell and I have difficulty understanding how inferred types
I am writing a Scheme-like in interpreter. It seems natural that the Scheme-like interpreter
I have a C# application that uses several web services which were added to
During my game loop I am constantly loading images using BitmapFactory.decodeResource(getResources(), R.drawable.objectx); Most of
Does anybody know the steps of haskell 'foldr' use of function? GHCI Command Window:
I have a folder that has a .pyc_dis file from a module. What can
Let's say I have the following function: sumAll :: [(Int,Int)] -> Int sumAll xs
Running the following program will print space overflow: current size 8388608 bytes. I have
I have a function (exercise 10.11 in Thompson's The Craft of Functional Programming )

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.