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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:07:34+00:00 2026-05-14T18:07:34+00:00

This is a homework question,I’m trying to do a Depth-first search function in Scheme,Here’s

  • 0

This is a homework question,I’m trying to do a Depth-first search function in Scheme,Here’s the code I’ve written so far:

(define explore
(λ(node visited)
  (let* ([neighbors             (force (cdr node))]
         [next        (nextNode visited neighbors)]
         [is-visited        (member? node visited)])

    (cond

      ;if I have no unvisited neighbours print current node and go up one level
      [(equal? next #f)
       (begin
         (display (car node))
         (display " "))]

      ;if current node is not visited and I have unvisited neighbors
      ;print current node,mark as visited and visit it's neighbors
      [(and (equal? is-visited #f) (not (equal? next #f)))
       (begin
         (display (car node))
         (display " ")
         (explore next (cons node visited)))])

    ;go and visit the next neighbor
    (if (not (equal? (nextNode (cons next visited) neighbors) #f ))
     (explore (nextNode (cons next visited) neighbors) (cons node visited))))))  

‘node’ is the current node
‘visited’ is a list in witch I keep track of the nodes I visited
‘nextNode’ is a function that returns the first unvisited neighbor if any or #f otherwise
‘member?’ test’s if a node is in the visited list

The Graph representation is using adjacent made using references to nodes with letrec so that’s why I use force in ‘neighbors’:
Eg:
(letrec ([node1 (list “NY” (delay (list node2 node3)))],where node2 and node3 are defined as node1

The problem witch I’m dealing with is that my visited lists looses track of some of the nodes I visited when I come out of recursion,How can I fix this ?

  • 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-14T18:07:35+00:00Added an answer on May 14, 2026 at 6:07 pm

    You have to get the new visited list as a returned value from your recursive calls. You’ll either have to modify explore so that it returns its visited list, or define a helper function that you recurse on instead of explore. Then after you recurse, you’ll have to use the new visited list that the function returns.

    EDIT:
    Perhaps a better way would be to just restructure your function. I think it’s more complicated than it needs to be. You’re just doing a depth-first traversal, right? Not really a search? You could try something more like this, using an explicit stack to keep track of nodes to visit, and a list of nodes visited:

    
    (define dft
      (lambda (graph)
        (helper graph '(1) '())))
    
    
    (define helper
      (lambda (graph stack visited)
        (if (empty? stack)
          '() ;we're done. you've got a nice list of visited nodes now, what will you do with it? Return it?
          (let ([currentNode (car stack)])
            (if (member? currentNode visited) 
                (helper graph 
                        ;what do you think the next two parameters are?
                        )
                (begin
                  (display currentNode)
                  (helper graph 
                          ;what do you think the next two parameters are?
                          ))))))
    
    

    Since this is a homework problem, I’ve left the two parameters for the helper function for you to fill in. The nicest thing about this approach is that it’s very simple to change it to a breadth-first traversal.

    Here’s a hint: the parameters for the two different cases will be slightly different.

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

Sidebar

Ask A Question

Stats

  • Questions 398k
  • Answers 398k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Problem: filter does lazy evaluation, so each new level of… May 15, 2026 at 3:32 am
  • Editorial Team
    Editorial Team added an answer You can use the descendant selector: <script type="text/javascript"> $(document).ready(function ()… May 15, 2026 at 3:32 am
  • Editorial Team
    Editorial Team added an answer When you are inserting, deleting or updating data in your… May 15, 2026 at 3:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.