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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:56:36+00:00 2026-05-18T08:56:36+00:00

High Guys, I have to define a polymorphic datatype for a tree that can

  • 0

High Guys,

I have to define a polymorphic datatype for a tree that can have multiple nodes. Each node can have any number of children and a vlaue. This type will always have at least one node. I am new in Haskell so am asking how can i declare the node to have variable number of arguments.

This is what i have now. This is a tree that can have a Node or a node with value (a) and two tree children. Instead of two tree children, i want them to be any number of tree children. (Analoog as java variable number of arguments “arg…”)

data Tree a = Node a | Node a (Tree a) (Tree a) deriving (Show)

Thanks for your help

EDIT

A little question::: How can i declare this node with variable arguments in a functions
parameter(header/signature). I have to implement a function called
“contains” which will check if a Node contains a specific element.

contains :: Tree a -> b -> Bool
contains (Node val [(tree)]) =   ......

Is the second line correct ?

  • 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-18T08:56:37+00:00Added an answer on May 18, 2026 at 8:56 am

    it would be:

    data Tree a = Node a | Node a [(Tree a)] deriving (Show)
    

    but in addition there is a second problem that this should be

    data Tree a = Leaf a | Branch a [(Tree a)] deriving (Show)
    

    or such as the parts of a union must have different names as otherwise you couldn’t use pattern matching

    Leaf and Branch are data constructors so:

    Branch 1 [Leaf 3, Branch 6 [Leaf 5]]
    

    is an example of a Tree


    contains :: Tree a -> a -> Boolean 
    contains (Leaf a) b = a == b
    contains (Branch a c) b = a == b || any (map (\t -> contains t b) c)
    

    or such

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

Sidebar

Related Questions

Any recommendations for a high-quality compact (no number pad) keyboard, preferably $50 or less?
I have been searching high and low for a way to get my silverlight
We have a high security application and we want to allow users to enter
Are there compilers for high-level languages (such as C) which can be targeted to
I am writing a high performance parser, and it seems to me that Int32.Parse
Some high risk data operations need to be logged. In this case, the high
So in high school math, and probably college, we are taught how to use
I need a high performance framework in native C++ for SQL. I need it
Given the extremely high requirements for unpredictability to prevent casinos from going bankrupt, what
I am part of a high school robotics team, and there is some debate

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.