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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:04:22+00:00 2026-06-14T03:04:22+00:00

I have this question to do: Define a function findpath:: BTree a -> a

  • 0

I have this question to do:

“Define a function findpath:: BTree a -> a -> Path (where Btree a is defined as in the previous questions) that, given a binary tree t and a value x, returns a path from the root of t to a leaf with value x if there is one, and the value Nothing otherwise. The running time of your program should be linear in the number of nodes in the tree.”

So far I have:

data Maybe a = Nothing | Just a
data BTree a = Leaf a | Fork (BTree a) (BTree a)
type Path = Maybe [Dir]
data Dir = Left | Right

findpath :: Eq a => BTree a -> a -> Path
findpath (Leaf y) x = if y==x then ??? else Nothing
findpath (Fork l r) x = nodepath (findpath l x) (findpath r x) where
   nodepath :: Path -> Path -> Path
   nodepath Nothing Nothing = Nothing
   nodepath Nothing pathr = Just [R]
   nodepath pathl Nothing = Just [L]

I still can’t work how to build the final answer in the (Leaf y) case

  • 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-14T03:04:23+00:00Added an answer on June 14, 2026 at 3:04 am

    Your language, about what you thought the program should do, suggests to me that you need help to escape from the trap of imperative thinking. Let me try to offer some help, based on thinking about what things are, not what things do.

    For findpath (Leaf y) x, you’re heading in the right direction. You just need to give if a lowercase i, and think about what the correct Path to a Leaf must be.

    Now, let’s think about the other possibility. You know more than that it’s some t. You know that you’re really trying to figure out what

    findpath (Node l r) x
    

    is (what it =, indeed), because that’s the other possibility for a BTree. Think of splitting the problem by asking “Is this BTree a (Leaf y) or a (Node l r)?” as one conceptual step of program design. Now, in order to figure out what the above left-hand side equals, you’re entitled to some recursively computed information, namely what

    findpath l x
    

    and

    findpath r x
    

    are. If you know Path information for both l and r, can you say what the Path for the whole Node l r is? Let me rephrase that question by writing it in Haskell:

    findpath :: Eq a => BTree a -> a -> Path
    findpath (Leaf y)   x = if y==x then ??? else Nothing
    findpath (Node l r) x = nodepath (findpath l x) (findpath r x) where
      nodepath :: Path -> Path -> Path
      nodepath ???
    

    I have expressed my question by introducing a helper function nodepath which takes as arguments the recursively computed information. Now you can try to implement nodepath by pattern matching on those two paths for the left and right subtrees, respectively. If you know whether they are (Just p) or Nothing, then you should be able to say what the path for the whole node must be.

    Lesson one, the useful thoughts are of the form: “If this is like such-and-such, then that must be so-and-so.”. Being, not doing.

    Lesson two, the basic method of programming over a datatype is: split into constructor cases (Leaf versus Node, Just versus Nothing); collect useful information from any substructures by recursive calls; say what the value for the whole structure must be.

    If you follow my advice and figure out what nodepath should be, you may find that it’s simple enough not to merit being a separate named definition. In that case, just replace the nodepath call with its meaning and cut out the where-clause. But it’s good to start by introducing nodepath, as it expresses a useful conceptual step towards solving the problem.

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

Sidebar

Related Questions

I have a route defined like this: GET /question/:q_id controllers.Questions.viewQuestion(q_id: Long) Then in my
I have found this question about the special function or in scheme: Joe Hacker
I have a question regarding the php validation, I have this script $(document).ready(function(){ //
So i have this txt file called Students.txt, I want to define a function
I have this question, just in theory. I do some query results and there
I have this question in which I have a SQL Server Compact Edition database
I have this question because I am not familiar latest generation of MS VS
I have this question: How could I call a codebehind method from jquery? I
I have this question relating to Lucene. I have a form and I get
I have this question, i am just throwing it out there. I am implementing

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.