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

I have a high CPU/memory bound task that I would like my Scala program
I have a high-traffic site that records web hits in a table, Hits. There's
This is a very high-level question. I'm looking for insight into this problem that
I agree with this answer and like it but i really would rather have
I'm looking for high-resolution timing code for iPhone, in order to do some performance
What are examples of high volume apps running atop of Google app engine?
I am reprogramming a high traffic website in asp.net mvc. The client gets about
From Don Syme blog ( http://blogs.msdn.com/b/dsyme/archive/2010/01/10/async-and-parallel-design-patterns-in-f-reporting-progress-with-events-plus-twitter-sample.aspx ) I tried to implement a twitter stream
Let me start from a real life example: Customer: Alex, just noticed something strange
I've the following problem, the following code: $link = $_GET['link']; $id = $_GET['block']; echo

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.