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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T21:13:52+00:00 2026-05-30T21:13:52+00:00

As the title says, I need this: getAllTrees :: [Int] -> [Tree Int] getAllTrees

  • 0

As the title says, I need this:

    getAllTrees :: [Int] -> [Tree Int]
    getAllTrees xs = undefined

where tree is

    data Tree x 
      = Null
      | Leaf x
      | Node (Tree x) x (Tree x)

I will appreciate any help, even the smallest clue 🙂
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-05-30T21:13:53+00:00Added an answer on May 30, 2026 at 9:13 pm

    I usually find it easiest to use the list monad for these kinds of problems. We can define getAllTrees by reasoning as follows:

    The only tree of zero items is Null:

    getAllTrees [] = return Null
    

    There is also only one tree of one element, namely a Leaf:

    getAllTrees [x] = return $ Leaf x
    

    When we have more than one element, we can split the list in all possible ways to determine how we should branch, and then recursively generate the sub-trees from each list. Let’s say we have a function splits :: [a] -> [([a], [a])] that returns all ways of splitting a list, for example:

    > splits [1..3]
    [([],[1,2,3]),([1],[2,3]),([1,2],[3]),([1,2,3],[])]
    

    We can then define the final case of getAllTrees by using the list monad. This allows us to write code which sort of looks like like we’re focusing on only one case, and the monad will give us all the combinations.

    getAllTrees xs = do
      (left, x : right) <- splits xs
      Node <$> getAllTrees left <*> pure x <*> getAllTrees right
    

    The first line splits the input list and takes the first item from the second part as the middle element. The case when the second part is empty doesn’t match the pattern, so it gets discarded since that’s how the list monad handles pattern match failures.

    The second line uses applicative syntax to say that we want the result to be a list of nodes, made from all combinations of sub-trees from the left list, the fixed middle element x, and all sub-trees from the right list.

    All that remains then is to implement splits. Looking at the example above, it’s easy to see that we can just take the inits and tails of the list and zip them together:

    splits xs = zip (inits xs) (tails xs)
    

    Time for a quick sanity check in the interpreter:

    > mapM_ print $ getAllTrees [1..3]
    Node Null 1 (Node Null 2 (Leaf 3))
    Node Null 1 (Node (Leaf 2) 3 Null)
    Node (Leaf 1) 2 (Leaf 3)
    Node (Node Null 1 (Leaf 2)) 3 Null
    Node (Node (Leaf 1) 2 Null) 3 Null
    > length $ getAllTrees [1..5]
    42
    

    Looks like we’re done! Some key lessons:

    • Try to think about the small cases first, and build up from there.
    • The list monad is useful for code that needs to generate all combinations of things.
    • You don’t have to do everything at once. Dealing with the list splitting separately made the code much simpler than it would have been otherwise.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As the title says, I need to write a small program to read data
as the title already says, I need to convert an int[] to a ByteBuffer
Just what the title says, I need to change the password for an existing
Basically what the title says... I need to have an image that when clicked,
As the title says, I need a notification when the content of a canvas
So, just like the title says, I need to create an application that gets
The title pretty much says it all. Some caveats are: I need to be
As the title says , I need to find two specific words in a
Like the title says, we need to set up WCF services between a .NET
As the title says, I need a search engine... for mysql searching. My website

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.