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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:38:06+00:00 2026-06-04T15:38:06+00:00

The following code is intended to produce either a Double or an Integer. s

  • 0

The following code is intended to produce either a Double or an Integer. s is assumed to be either negate or id; n the whole part; and f the fractional part or Nothing for an integer.

computeValue :: Num a => (a->a) -> Integer -> (Maybe Double) -> Either Double Integer
computeValue s n Nothing = Right $ s n
computeValue s n (Just a) = Left $ s (fromIntegral n + a)

when I compile this I get:

test1.hs:2:28:
    Couldn't match type `Integer' with `Double'
    Expected type: Either Double Integer
      Actual type: Either Double a
    In the expression: Right $ s n
    In an equation for `computeValue':
        computeValue s n Nothing = Right $ s n

test1.hs:2:38:
    Couldn't match type `Integer' with `Double'
    In the first argument of `s', namely `n'
    In the second argument of `($)', namely `s n'
    In the expression: Right $ s n

It seems like somehow the compiler has lost track of the fact that s is polymorphic. What happened here and how do I fix it?

  • 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-06-04T15:38:07+00:00Added an answer on June 4, 2026 at 3:38 pm

    s is not polymorphic from inside of your function: you can use any function that works on some Num instance as this parameter, it might be a function that only works on Complex! What you need is an universally quantified function s, i.e. one that can actually be called with any Num instance.

    {-# LANGUAGE Rank2Types #-}
    
    computeValue :: (forall a . Num a => a->a) -> Integer -> Maybe Double -> Either Double Integer
    computeValue s n Nothing = Right $ s n
    computeValue s n (Just a) = Left $ s (fromIntegral n + a)
    

    That works then:

    Prelude Data.Either> computeValue id 3 Nothing
    Right 3
    Prelude Data.Either> computeValue negate 57 (Just pi)
    Left (-60.1415926535898)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code should exhibit intended functionality: <head> <style> a:active { color:teal; } a:hover
consider the following code, which represents an attempt to implement partial matching. the intended
The following code is intended to retrieve a file via FTP. However, I'm getting
I am having issues with the following part of my code. when nn is
Following code does destroy records as intended, but the callback is inherited from one
I'm not sure why the following code is not working as intended: litMessage.Text =
The following code is intended to do a purely ajax POST request, instead it
I have a problem with the following code fragment. It's intended to handle events
Can anyone provide either a method of using the following code that doesn't result
The following code is intended to dump a long list of numbers from a

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.