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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:57:48+00:00 2026-05-28T06:57:48+00:00

I tried writing a simple function that takes an Either type (possibly parameterized by

  • 0

I tried writing a simple function that takes an Either type (possibly parameterized by two different types) and does one thing if it gets Left and another thing if it gets Right. the following code,

someFunc :: (Show a, Show b) => Either a b -> IO ()
someFunc (Left x) = print $ "Left " ++ show x
someFunc (Right x) = print $ "Right " ++ show x

main = do
  someFunc (Left "Test1")
  someFunc (Right "Test2")

However, this gives,

Ambiguous type variable `b0' in the constraint:
      (Show b0) arising from a use of `someFunc'
    Probable fix: add a type signature that fixes these type variable(s)
    In a stmt of a 'do' expression: someFunc (Left "Test1")

and

Ambiguous type variable `a0' in the constraint:
      (Show a0) arising from a use of `someFunc'
    Probable fix: add a type signature that fixes these type variable(s)
    In the expression: someFunc (Right "Test2")

If I understand correctly, when I call the function with Left x, it is complaining because it doesn’t know the type of the Right x variant, and vice-versa. However, this branch of the function is not used. Is there a better way to do this?

  • 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-28T06:57:49+00:00Added an answer on May 28, 2026 at 6:57 am

    Well, that depends heavily on what you are trying to do, doesn’t it? As you already found out, in order to use Left constructor, you need to know the type it constructs. And full type requires information on both a and b.

    Better way to achieve polymorphism in Haskell is to use type classes. You can easily provide different implementations of “methods” for different instances.

    Good comparison of both object-orientation and type classes concepts can be found here.

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

Sidebar

Related Questions

I'm trying to learn Lisp (elisp, actually), and I tried writing the following function
I'm writing an app for WM that handles incoming SMS events. I tried making
So I'm writing a simple page that I want to be able to slide
soooo i have this function that prints one of 5 random texts ON an
I am writing a simple bookmarklet that runs on youtube page and fetch some
I have a very simple function which takes in a matching bitfield, a grid,
I am having trouble with writing a simple(!) PHP function to return an array
I'm currently writing a simple app to compare two lists and return differences if
I tried writing a program in Java using regex to match a pattern and
I have tried the following program for writing the contents into Spreadsheet. I downloaded

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.