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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:53:42+00:00 2026-06-12T06:53:42+00:00

I need to make the following data type an instance of Show : data

  • 0

I need to make the following data type an instance of Show:

data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a 

I’m fairly new at this, but to start with, I interpreted this declaration as

“We have made a new type called Tree, which is parameterized with types a and b. A tree can be one of two things: a Branch which holds a piece of data of type b, plus two more Trees, or a Leaf which holds a data item of type a.”

Now, I need to make a way to “Show” it nicely (nested branches, etc.), without using deriving. So far, I have only been writing functions in a module Main and loading/playing with them in an interpreter window, so I haven’t actually done things with constructors, etc, before. Nevertheless, I figured I could just start off by declaring the tree data type within my file, as shown at the beginning of the question, and go from there.

As I messed around with “Show” without much success, I thought maybe I needed to define a small component of the tree and how to “Show” it first, before trying to work with the whole tree:

data Leaf a = Leaf a

instance Show (Leaf a) where
show (Leaf a) = ??? 

I tried a number of things in the ??? spot, such as “a”, just a by itself, putStrLn, etc., but none are printing out the value of a when I say something like

>show (Leaf 3)

In fact, I have in many cases encountered this, which probably means I am not locating things right:

Ambiguous occurrence `show'
    It could refer to either `Main.show', defined at a2.hs:125:1
                          or `Prelude.show',
                             imported from `Prelude' at a2.hs:2:8-11
                             (and originally defined in `GHC.Show')

…which I addressed by calling “Main.show,” which of course doesn’t work.

I guess the question is, where do I go with all this…or maybe just, “How can I fix the Leaf “Show” utility so that I can figure out how to extend it?” (assuming I have to define it first…)

  • 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-12T06:53:43+00:00Added an answer on June 12, 2026 at 6:53 am

    You have to start like that:

    data Tree a b = Branch b (Tree a b) (Tree a b) | Leaf a
    
    instance (Show a, Show b) => Show (Tree a b) where
        show (Leaf x) = show x
        show (Branch p l r) = ???
    

    In order to show a Tree a b you have to first be able to show as and bs. That’s what the (Show a, Show b) => part does, it specifies the preconditions you require for your instance to work.

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

Sidebar

Related Questions

Following is the sample data. I need to make 3 copies of this data
I need to make a point in time branch, when we release a new
I need a checkbox but the underlying data is of type smallint in the
I need to make a query output Using table, I want the following report,
I need make some action (dump statistical data) before the Dart program ends. The
I have the following jQuery/ajax script: jQuery(input.button).click(function(){ jQuery.ajax({ url: 'addfav.php', type: 'POST', data: {'id':jQuery(this).closest(div).attr(id),is_ajax:
First attempt It's difficult to make this question pithy, but to provide a minimal
i need to make put request, to /users/upload_image action. Following code: <% form_remote_for @user,
I need make all of my posts update. I use bulk upload for store,
Need to make certain Ruby strings in my program to be immutable. What is

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.