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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:35:36+00:00 2026-05-28T18:35:36+00:00

I am working on the problem for writing Haskell code similar to a C++

  • 0

I am working on the problem for writing Haskell code similar to a C++ program.

The C++ code is:

class Rectangle
{
    private:
        int length;
        int width;
    public:
        Rectangle()
        {
            length = 0;
            width = 0;
        }
        Rectangle(int x)
        {
            length = x;
            width =0;
        }
        Rectangle ( int x , int y)
        {
            length = x;
            width = y;
        }
};

To write similar Haskell code i made a data type Rectangle

data Rectangle = Rectangle Length Width deriving (Eq, Show , Read)
type Length = Int
type Width = Int

Then i thought of making a load function which can act as constructor. But i don’t understand how to implement function overloading with different number of arguments.
Please help. 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-28T18:35:38+00:00Added an answer on May 28, 2026 at 6:35 pm

    While it is possible to do such overloading in Haskell, it’s not considered idiomatic, and will likely lead to confusing errors later on. Instead, you should simply define functions that construct data:

    point :: Rectangle
    point = Rectangle 0 0
    
    line :: Length -> Rectangle
    line l = Rectangle l 0
    
    square :: Int -> Rectangle
    square a = Rectangle a a
    

    This allows you to give clear names that describe the semantics of each overloading, rather than relying on the number and type of arguments given to disambiguate which you mean.

    However, if you do want to write the overloaded version, you can do it easily with typeclasses:

    class MakeRectangle a where
      rectangle :: a
    
    instance MakeRectangle Rectangle where
      rectangle = Rectangle 0 0
    
    instance MakeRectangle (Length -> Rectangle) where
      rectangle l = Rectangle l 0
    
    instance MakeRectangle (Length -> Width -> Rectangle) where
      rectangle = Rectangle
    

    You’ll need {-# LANGUAGE FlexibleInstances #-} at the top of your file to compile this. A trick like this is used by the standard Text.Printf library, but I would not consider it a particularly good example of overloading in Haskell; there is almost always some structure to the overloaded value’s type, whereas here its entire structure is dictated by the instance, which can get in the way of type inference; not only that, but there aren’t any reasonable laws that govern instances (indeed, the type is too general to permit any).

    But if you really want to do it, you can, and while it’s usually a bad idea, sometimes (as in the case of printf) it’s the only way to accomplish the interface you want.

    To try this out in GHCi, you’ll need to specify the types you’re using explicitly, or it won’t be able to resolve the instances:

    GHCi> rectangle :: Rectangle
    Rectangle 0 0
    GHCi> rectangle (1 :: Length) :: Rectangle
    Rectangle 1 0
    GHCi> rectangle (1 :: Length) (2 :: Width) :: Rectangle
    Rectangle 1 2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on writing some automation code and I'm running into a problem finding
I'm working on writing a Pong game and I've run across a problem. I'm
I've written this small test class just to illustrate my problem When working with
I am writing a program to try to solve a math problem. I need
I'm having a strange problem with the following code, I'm writing a game launcher
writing to a text file is working good...But my problem is text file is
I am writing a calendar-based web application and am working on some code that
I am working on writing some code to scrub user input to my ASP.NET
I'm working with reflection read and writing objects. I have the problem that I
I'm writing a windows application in C++ and encountered the following problem when working

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.