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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:15:37+00:00 2026-05-18T07:15:37+00:00

I have finally figured out how to generate a random number, but the compiler

  • 0

I have finally figured out how to generate a random number, but the compiler still complains on my code.

Code:

type Quot   = Integer

sign (p,q,g) (x,pub) d =
       do k <- randomRIO(1,q-1 :: Integer)
   ki <- kinv k q
   r <- g^k `mod` p
   s <- (160 + x*r)*ki mod q
   return (r,s)

kinv :: Integer -> Quot -> Integer
kinv k q =
   do (d,t,s) <- gcdE(q,k)
      return s

gcdE(a,0) = (a,1,0)
gcdE(a,b) = (d,t,s - q*t)
    where r = a `mod` b
         q = a `div` b
        (d,s,t) = gcdE(b,r)

Error message:

crypt.hs:24:7:
Couldn't match expected type `Integer' against inferred type `m a'
In a stmt of a 'do' expression: ki <- kinv k q
In the expression:
    do { k <- randomRIO (1, q - 1 :: Integer);
         ki <- kinv k q;
         r <- g ^ k `mod` p;
         s <- (160 + x * r) * ki mod q;
         .... }
In the definition of `sign':
    sign (p, q, g) (x, pub) d
           = do { k <- randomRIO (1, q - 1 :: Integer);
                  ki <- kinv k q;
                  r <- g ^ k `mod` p;
                  .... }

crypt.hs:37:10:
Couldn't match expected type `Integer'
       against inferred type `(Quot, Quot, b)'
In a stmt of a 'do' expression: (d, t, s) <- gcdE (q, k)
In the expression:
    do { (d, t, s) <- gcdE (q, k);
         return s }
In the definition of `kinv':
    kinv k q
           = do { (d, t, s) <- gcdE (q, k);
                  return s }

All I want is for the variable k to take a random integer value between 0 < k < q. I am not so good with Haskell so this has taken a while. 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-18T07:15:38+00:00Added an answer on May 18, 2026 at 7:15 am

    Since there’s nothing monadic about kinv or gcdE, neither of them should contain any dos, <-s or returns.

    Similarly you don’t need to (and can’t) use <- to get the result of kinv and gcdE. The same is true for any arithmetic operators. Just use let result = non-monadic operation, not result <- non-monadic operation.

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

Sidebar

Related Questions

I finally figured out what's wrong with my code, but I'm not sure how
I have figured out a lot about nhibernate, but this seems to be my
I have finally figured out how to use the apple Reachability files, which is
After wasting some time to figure out what goes wrong, I finally have to
I have finally worked out how to get stdin and stdout to pipe between
I think I finally figured out they need to use this DeclarativeFieldsMetaclass (to turn
I finally figured out a good/easy way to make clean URLs with regex on
Alright, I finally figured out how to write my queries using MySQL. Does anyone
I've finally figured out how to make all my millions of tabs (not that
I have finally finished my web site. I published it, and I was surprised

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.