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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:06:47+00:00 2026-05-22T21:06:47+00:00

I am trying to write a function in which records all paths that lead

  • 0

I am trying to write a function in which records all paths that lead to data N.

can anyone give me some pointers as im getting confused, when do i record the path? as if i start at the beginning i may end up with paths that lead to know where. (Recording paths as L | R)

Can anyone give me some logic on this!

Thanks

I have worked on trees with a given path, but this i cant figure out

data Tree = N | F Tree Tree deriving Show
data Dir = L | R deriving Show
type Path = [Dir]       

so a tree could be F N (F (F N N) (F N (F N N)))
and a path would be [L,L,L,R]

I have made functions to insert where there are N nodes or at a given path.

But i cant get my head round the logic of recording the paths.

  • 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-22T21:06:47+00:00Added an answer on May 22, 2026 at 9:06 pm
    main = print . findAllLeaves $ F N (F (F N N) (F N (F N N)))
    
    data Tree = N | F Tree Tree deriving Show
    data Dir = L | R deriving Show
    type Path = [Dir]    
    
    descend :: Tree -> Dir -> Tree
    descend (F l _) L = l
    descend (F _ r) R = r
    descend _ _ = undefined
    
    findAllLeaves :: Tree -> [Path]
    findAllLeaves N = [[]]
    findAllLeaves tree = do dir <- [L, R]
                            map (dir:) $ findAllLeaves (descend tree dir)
    

    Result

    [[L],[R,L,L],[R,L,R],[R,R,L],[R,R,R,L],[R,R,R,R]]
    

    I use the list monad to pick both L and R “simultaneously”, and descend both branches. Gotta love nondeterminism!


    Explanation:

    I hope descend is clear enough. You give it a tree and a direction, and it descends the tree in that direction.

    findAllLeaves is the interesting one. How does it work?

    We’ll talk about the base case, findAllLeaves N = [[]], in a minute.

    The recursive case is written in the list monad, with do notation. The first line is simple: choose L or R and assign this to dir. The list monad will actually choose both, and take the results for each and concatenate them together. This is they key thing to understand. It’s exactly what you asked for: What are all the paths starting with L, and what are all the paths starting with R? Put those together and you have all the paths from the current node to its descendant leaf nodes.

    The second line should be fairly clear from the previous paragraph’s description. Descend the tree in the given direction (descend tree dir), find all leaves from that point (findAllLeaves), and then prepend the direction chosen to each of those subpaths (map (dir:)).

    So why the base case [[]]? Well, think about the case just above the base case. So, for example, findAllLeaves (F N N). When we choose dir = L, we evaluate the second line:

    map (L:) $ findAllLeaves (descend (F N N) L)
    

    Descending left gives us just N

    map (L:) $ findAllLeaves N
    

    Then we’ve hit the base case:

    map (L:) $ [[]]
    

    Now can you see why we have that weird base case? A list with an empty list inside? Because we are going to map (L:) onto it, in other words, prepend L to each list in the outer list. This results in:

    [[L]]
    

    We get a similar result when dir = R.

    [[R]]
    

    So then the list monad concatenates those together

    [[L]] ++ [[R]]
    

    And we end up with

    [[L], [R]]
    

    If any of this is still unclear, please let me know in a comment and I’ll try to clarify.

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

Sidebar

Related Questions

I'm trying to write a function that can call a webmethod from a webserive
I'm trying to write a function that reads files from a deferred directory which
hello everyone I'm trying to write function which can unfold int in the list
Hopefully someone can help out here; I am trying to write a function which
I am trying to write a function which can take any of the standard
I am trying to write a template function that accepts a std::function which depends
I am trying to write a function which can tell me whether there is
I'm trying to write a function that formats every (string) member/variable in an object,
I am trying to write a function that will pull the name of a
I'm trying to write a function that is able to determine whether a string

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.