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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:29:16+00:00 2026-06-02T13:29:16+00:00

I have a hobby project which is about creating a tree to store identification

  • 0

I have a hobby project which is about creating a tree to store identification numbers. I had used digit, stored at node, that is node can be 0 1 2 3 4 5 6 7 8 9.

After I have create tree, I want compose list from tree. But, I could not find a algorithm to manage my goal.

What I want :

      "recompose tree"  will return list of numbers. For below tree it should be 

            [ 2, 21, 243, 245, 246, 78, 789 ]  


                               Root 
                           /          \         
                        2*               7
                      /      \             \                  
                   1*         4              8*    
                            / \  \            \         
                           3*  5* 6*           9*

       my data type :  data ID x = ID ( ( x, Mark ), [ ID x ] ) 
                       data Mark = Marked | Unmarked

       EDIT:

       for convenience : * shows it is marked
                         I have stored digit as char, actually not 1, 
                            it is stored as'1' 

Do you have advice how I can do that ? ( advice is prefferred to be algorithm )

  • 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-02T13:29:20+00:00Added an answer on June 2, 2026 at 1:29 pm

    What about

    recompose :: Num a => ID a -> [a]
    recompose = go 0
      where
        go acc (ID ((n, Marked), ids)) =
          let acc' = 10 * acc + n
          in  acc' : concatMap (go acc') ids
        go acc (ID ((n, Unmarked), ids)) =
          let acc' = 10 * acc + n
          in  concatMap (go acc') ids
    

    ?

    That is, we traverse the tree while accumulating a value for the path from the root to a node. At every node we update the accumulator by multiplying the value for the path by 10 and adding the value for the node to it. The traversal produces a list of all accumulator values for marked node: so, at marked node we add the accumulator value to the list, for unmarked nodes we just propagate the list that we have collected for the children of the node.

    How do we compute the list for the children of a node? We recursively call the traversal function (go) to all children by mapping it over the list of children. That gives us a list of lists that we concatenate to obtain a single list. (concatMap f xs is just concat (map f xs)) or concat . map f.)

    In attribute-grammar terminology: the accumulator serves as an inherited attribute, while the returned list is a synthesised attribute.

    As a refinement, we can introduce an auxiliary function isMarked,

    isMarked :: Mark -> Bool
    isMarked Marked   = True
    isMarked Unmarked = False
    

    so that we can concisely write

    recompose :: Num a => ID a -> [a]
    recompose = go 0
      where
        go acc (ID ((n, m), ids)) =
          let acc'   = 10 * acc + n
              prefix = if isMarked m then (acc' :) else id
          in  prefix (concatMap (go acc') ids)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an idea for a hobby project which performs some code analysis and
I have been asked to lend a hand on a hobby project that a
I am teaching myself to use JavaCC in a hobby project, and have a
I have recently started using PlayN for a hobby project and has now stumbled
I have a hobby project that includes me driving a LCD by my serial
I have a hobby project that is written in C# using MonoDevelop. I've been
I have a good sized hobby project, around 66K LOC, over at http://www.wheelmud.net Recently,
Original Question: Hello, I am creating very simple hobby project - browser based multiplayer
I have a small hobby project where I'm writing a 'wrapper'-daemon in linux, in
Im working on a little hobby project. I already have written the code to

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.