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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:58:41+00:00 2026-06-04T22:58:41+00:00

This is my first attempt at creating a custom instance of a class such

  • 0

This is my first attempt at creating a custom instance of a class such as Ord.

I’ve defined a new data structure to represent a list:

data List a = Empty | Cons a (List a)
    deriving (Show, Eq)

Now I want to define a new instance of Ord for List such that List a <= List b implies “the sum of the elements in List a is less than or equal to the sum of the elements in List b”

first of all, is it necessary to define a new “sum” function since the sum defined in Prelude wont work with the new List data type? then, how do I define the new instance of Ord for List?

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-06-04T22:58:42+00:00Added an answer on June 4, 2026 at 10:58 pm

    First of all, this won’t work exactly like the normal list instance. The normal instance only depends on the items of the list being orderable themselves; your proposal depends on their being numbers (e.g. in the Num class) and so is more narrow.

    It is necessary to define a new sum function. Happily it’s very easy to write sum as a simple recursive function. (Coincidentally, you can call your function sum', which is pronounced as “sum prime” and by convention means it’s a function very similar to sum.)

    Additionally, the instance would have to depend on the Num class as well as the Ord class.

    Once you have a new sum function, you can define an instance something like this:

    instance (Ord n, Num n) => Ord (List n) where compare = ... 
      -- The definition uses sum'
    

    This instance statement can be read as saying that for all types n, if n is in Ord and Num, List n is in Ord where comparisons work as follows. The syntax is very similar to math where => is implication. Hopefully this makes remembering the syntax easier.

    You have to give a reasonable definition of compare. For reference, compare a b works as follows: if a < b it returns LT, if a = b it returns EQ and if a > b it returns GT.

    This is an easy function to implement, so I’ll leave it as an exercise to the reader. (I’ve always wanted to say that :P).

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

Sidebar

Related Questions

This is my first attempt at creating a service and incorporating it in my
I'm using Visual Web Developer 2010 Express. This is my first attempt at creating
This is my first attempt at creating a Windows service that calls multiple threads.
This is my first attempt at creating a package, so I must be missing
This is my first ever attempt at creating an svn repository and I'm confused.
this if my first attempt at using streaming for WCF, and I am struggling
This is my first attempt to create a GUI in MATLAB. I haven't been
this is my first attempt at a responsive design so excuse me if this
Because this is my first attempt at an extension method that seems quite useful
This is pretty strange (admitedly, this is my first attempt with python / sqlite),

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.