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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:03:50+00:00 2026-05-27T21:03:50+00:00

I am reading through LYAH, and in Chapter 9, I found a curious problem.

  • 0

I am reading through LYAH, and in Chapter 9, I found a curious problem. The author provides an example of implementing the “randoms” function:

randoms' :: (RandomGen g, Random a) => g -> [a]
randoms' gen = let (value, newGen) = random gen in value:randoms' newGen

Well, this compiles just fine. But if I change the second line to:

randoms' gen = (fst (random gen)) : (randoms' (snd (random gen)))

The this file reports error on loading:

IOlesson.hs:4:52:
    Ambiguous type variable `a' in the constraint:
      `Random a' arising from a use of `random' at IOlesson.hs:4:52-61
    Probable fix: add a type signature that fixes these type variable(s)
Failed, modules loaded: none.

If I change this line to:

randoms' gen = (fst (random gen)) : (randoms' gen)

Then this will do just fine, and as expected, this will return a list of all identical elements.

I am puzzled: What’s so different in Miran’s version and my version?

Thanks for any ideas!

  • 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-27T21:03:51+00:00Added an answer on May 27, 2026 at 9:03 pm

    The problem is that random takes any instance of RandGen, and returns a random value and a new generator of the same type. But the random value can be any type with an instance of Random!

    random :: (Random a, RandomGen g) => g -> (a, g)
    

    So, when you call random for the second time in the recursion, it doesn’t know what the type of the first element should be! True, you don’t really care about it (you throw it away with snd, after all), but the choice of a can affect the behaviour of random. So to disambiguate, you need to tell GHC what you want a to be. The easiest way is to rewrite your definition as follows:

    randoms' gen = let (value, gen') = random gen in value : randoms' gen'
    

    Because you use value as part of the resulting list, it’s forced to have the same type as the a in your type signature — the element type of the resulting list. The ambiguity is resolved, and the duplicate computation of the next random number is avoided, to boot. There are ways to disambiguate this more directly (keeping the duplicate computation), but they’re either ugly and confusing or involve language extensions. Thankfully, you shouldn’t run into this very often, and when you do, a method like this should work to resolve the ambiguity.

    Equivalently and perhaps more neatly, you can write:

    randoms' gen = value : randoms' gen'
      where (value, gen') = random gen
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Reading through the C specs I found this function: double remquo(double x, double y,
Reading through documentation, I found following: 1.9.1 1.8.4 1.8.2 A version of 1.8.2 select
I have been reading through the C++ FAQ and was curious about the friend
after reading through a couple similar Qs/As I haven't quite found the solution I'm
I was reading through K&R and i came across this example about uncertainty in
Reading through Thinking in Java i stuck in ex:6 of Inner Classes chapter. Exercise
I'm reading through a fantastic article on the .NET Configuration namespace (found here )
Reading through a paper for the ARM Cortex-M3 CPU I found this line confusing:
I was reading through the answers earning a reversal badge and I found a
Reading through this , I came to the bit on default values for function

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.