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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:47:47+00:00 2026-05-21T03:47:47+00:00

Im trying to make a funciton which allows me to add a new value

  • 0

Im trying to make a funciton which allows me to add a new value to a tree IF the value at the given path is equal to ND (no data), this was my first attempt.

It checks the value etc, but the problem, is i want to be able to print the modified tree with the new data. can any one give me any pointers? I have also tried making a second function that checks the path to see if its ok to add data, but im just lost to how to print out the modified tree?

  • 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-21T03:47:48+00:00Added an answer on May 21, 2026 at 3:47 am

    As iuliux points out, your problem is that you are treating your BTree as though it were a mutable structure. Remember functions in haskell take arguments and return a value. That is all. So when you “map over” a list, or traverse a tree your function needs to return a new tree.

    The code you have is traversing the recursive tree and only returning the last leaf. Imagine for now that the leaf at the end of the path will always be ND. This is what you want:

    add :: a -> Path -> Btree a -> Btree a
    add da xs ND = Data da
    add _  [] _  = error "You should make sure this doesn't happen or handle it"
    add da (x:xs) (Branch st st2) = 
                   case x of 
                        L -> Branch (add da xs st) st2
                        R -> Branch st (add da xs st2)
    

    Notice how in your original code you discard the Branch you pattern match against, when what you need to do is return it “behind you” as it were.


    Now, on to the issue of handling situations where the leaf you arrive it is not a ND constructor:

    This type of problem is common in functional programming. How can you return your recursive data structure “as you go” when the final result depends on a leaf far down the tree?

    One solution for the trickiest of cases is the Zipper, which is a data structure that lets you go up down and sideways as you please. For your case that would be overkill.

    I would suggest you change your function to the following:

    add :: a -> Path -> Btree a -> Maybe (Btree a)
    

    which means at each level you must return a Maybe (Btree a). Then use the Functor instance of Maybe in your recursive calls. Notice:

    fmap (+1) (Just 2) == Just 3
    fmap (+1) (Nothing) == Nothing
    

    You should try to puzzle out the implementation for yourself!

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

Sidebar

Related Questions

Trying to make a make generic select control that I can dynamically add elements
Trying to make a generic PL/SQL procedure to export data in specific XML format,
I'm trying to make a data bound column invisible after data binding, because it
I'm trying to make a autoscrolling/carousel like function for an unordered list of images.
Trying to make a MySQL-based application support MS SQL, I ran into the following
Trying to make a web service call to an HTTPS endpoint in my Silverlight
I'm trying to make the case for click-once and smart client development but my
I'm trying to make a context menu for a control that is linked to
I am trying to make a div, that when you click it turns into
I am trying to make a JTable that has column spans available. Specifically, I

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.