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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:25:18+00:00 2026-06-15T22:25:18+00:00

Define recurrent type of Integers representation to work like so: data Integers = Zero

  • 0

Define recurrent type of Integers representation to work like so:

data Integers = Zero | Next Integers | Prev Integers

and make this representation, the instance of class Num, it means that You should can use (+), (*), (==), abs, signum, show on Integers

Till now i defined sth like this:

data Integers = Zero | Integers Int deriving (Show)

next :: Integers -> Integers
next Zero = Integers 1
next (Integers a) = Integers a + Integers 1

prev :: Integers -> Integers
prev (Integers 1) = Zero
prev (Integers a) = Integers a - Integers 1

instance Eq Integers where  
    Zero == Zero = True
    Integers a == Integers b = a == b  
    _ == _ = False

instance Num Integers where
    Integers a + Integers b = Integers (a + b)
    Integers a - Integers b = Integers (a - b)
    Integers a * Integers b = Integers (a * b)
    abs (Integers a) = Integers (abs a)
    signum (Integers a) = Integers (signum a)
    fromInteger a = Integers (fromInteger a)

But it doesn’t fit the data Integers = Zero | Next Integers | Prev Integers expectations

  • 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-15T22:25:19+00:00Added an answer on June 15, 2026 at 10:25 pm
    data Integers = Zero | Next Integers | Prev Integers
    

    I’m going to show you +, the rest should be easy enough.

    Zero + y = y
    x + Zero = x
    

    Well, that was easy!

    Oh. There are some other cases.

    Still, we’ve handled all the Zero cases, so now we only have to deal with Prev and Next. They’re opposites of each other, aren’t they? So if we’re given one of each, they’ll cancel each other out.

    Next x + Prev y = x + y
    Prev x + Next y = x + y
    

    Now we only have to worry about the cases where the numbers we’re given both have the same sign.

    Next x + Next y = Next (Next (x + y))
    Prev x + Prev y = Prev (Prev (x + y))
    

    (These last two equations are not the most efficient implementation, but they are straightforward to understand.)

    And we’re done defining +.

    Some of the other functions are easier, some are harder (and should reuse some of the easier functions), but they all involve pattern matching on the/either/both parameter(s) and doing the appropriate thing. And mostly they involve recursion, usually inescapable at some level when given a recursive data structure.

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

Sidebar

Related Questions

I define fonts on their own like this: .smallboldblue{font-family:Trebuchet MS;font-weight:bold;font-size:11px;color:#3A63A5;} .medregblue{font-family:Trebuchet MS;font-size:13px;color:#3A63A5;} .medboldblue{font-family:Trebuchet MS;font-weight:bold;font-size:13px;color:#3A63A5;}
#define offsetof(type, member) ((size_t)(&((type *)0)->member)) I am not understanding (&((type *)0)->member) what is this
I keep looking, but it seems like there's zero interest from compiler developers in
Define a list: nat2::[(Integer, Integer)] that contains all pairs of nonnegative integers ordered by
New to sql and php and trying to work through this one. After reading
#define BLAH word cout << BLAH; Is there any way to do this?
#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, native-activity, __VA_ARGS__)) #define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, native-activity, __VA_ARGS__)) This is definition for
#define ROW 3 #define COL 4 class Matrix { private: int mat[ROW][COL]; //..... //.....
#define ing _GLIBCXX_DEBUG forces GCC to catch a large class of runtime errors in
#define START ((void (**)(int)) 0x0fff) *START = &fun_foo(); I haven't seen this before. What

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.