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

  • Home
  • SEARCH
  • 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 9267365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:32:02+00:00 2026-06-18T14:32:02+00:00

I have a function that takes in some data and returns an array of

  • 0

I have a function that takes in some data and returns an array of values. I need to map a list of data onto a tree.

As I create the tree each call to my function requires three parameters:

  • A character from the string
  • The index of the character from the string
  • Part of the output of the function for the node’s parent (or zero for the root)

For the sake of argument let’s say I have:

input = "ABC"

func :: (Char, Int, Int) -> [(Char, Int, Int)]
func ('A', 1,    0) = [('Q', 1, 1243)]
func ('B', 2, 1243) = [('D', 2, 7512), ('R', 2, 8253)] -- 1243 taken from above
func ('C', 3, 7512) = [('E', 3, 2765)]
func ('C', 3, 8253) = [('Z', 3, 9836)]

Which would map to a tree like:

    ('Q', 1243)
     /       \
('D',7512)  ('R',8253)
    |         |
('E',2765)  ('Z',9836)

The first two parameters are fine, I can get those before building the list:

input `zip` [1..]

I’m not sure how to go about getting the third parameter though, since I only know the value for the root node (which will be zero) before I start building the tree. Am I going to have to learn about Monads?

Note: I’m completely new to Haskell and to Functional Programming in general.

  • 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-18T14:32:03+00:00Added an answer on June 18, 2026 at 2:32 pm

    Is something like this what you’re after?

    import Data.Tree
    
    buildForest :: String ->
                   ((Char, Int, Int) -> [(Char, Int, Int)]) ->
                   Forest (Char, Int)
    buildForest input children = go input 1 0
        where
            go []     _ _ = []
            go (x:xs) i n = map transform (children (x,i,n))
                where
                    transform (y,_,n') = Node (y,n') (go xs (i+1) n')
    

    Then with your example you’d call it like buildForest "ABC" func.

    (Code untested, but if it’s wrong you should still be able to use the general approach.)

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

Sidebar

Related Questions

I have a function that takes an array of objects and I need to
I am making an ajax call that returns some json data. I need to
I have an array-map which I am assoc ing some values into it. After
Lets say I have a function which takes some input structure and returns some
I have a function that takes an input string and then runs the string
I have a function that takes a By element, By by = By.xpath(xpathString). Inside
I have a function that takes a string-like argument. I want to decide if
I have a function that takes a const D3DVECTOR3 *pos , but I have
I have a function that takes 2 parameters : 1 = XML file, 2
I have a function that takes variadic arguments, that I obtain from func_get_args() .

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.