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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:33:20+00:00 2026-06-03T21:33:20+00:00

I’m pretty new to Scheme and I’m having trouble writing a function as part

  • 0

I’m pretty new to Scheme and I’m having trouble writing a function as part of a homework. I have a graph G given to me as a list in the following format: ((node1 node2 weight1) (node3 node4 weight2) …). I’m trying to write a function that returns me the list of all nodes (V) in this graph in this format: (node1 node2 node3 …). The function may only take the graph (G) as input.

So I thought I can do this recursively by adding the first and second elements of each nested list within G, to V. Here’s what I have written:

    (define nodes-of
      (lambda (G)
        (if (null? G) 
            ()
            (begin (add-to-set (cadar G) (nodes-of (cdr G))) 
                   (add-to-set (caar G) (nodes-of (cdr G))))))

I think this is wrong though, as the first recursion only involves (cadar G) and the second involves (caar G), and the return value will be set only by the second statement under begin (if I’m not mistaken).

add-to-set is a function that I’ve written earlier for the homework, it adds an element to a list if it doesn’t already exist in the list. (eg: add-to-set n S , this will add n to S)

Can anybody help me with this? (btw I’m not allowed to use multiple let’s, let* or set)

  • 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-03T21:33:22+00:00Added an answer on June 3, 2026 at 9:33 pm

    Well, you are right that you can do it recursively, and your code is pretty close. Recall that every recursive procedure requires a base state for which you know the answer, and a way to reduce the complexity of the problem each time you recurse.

    In the case of processing a list your base case is the empty list, which is why you want to check for null. Then the reduction formula will be breaking off a piece of the list then calling the procedure on the remaining part. So, like I said, you are close.

    Realize then, that since your data is regularly structured you can just treat your graph as a normal list. You don’t need to recurse into every element of the list, you just want to perform two actions on every element and put the results in a list.

    (define nodes-of
        (lambda (G)
            (if (null? G)   ;<-- have we reached the base case yet?
                '()         ;<-- if yes, return null so our cons will build a list
                (cons (caar G) (cons (cadar G) (nodes-of (cdr G))))))) ;<-- if not, keep building the list by grabbing the things we want from each element, then reducing the list
    

    You could also use a let if you wish..

    (define nodes-of
        (lambda (G)
            (if (null? G)
                 '()
                 (let ((n1 (caar G))
                       (n2 (cadar G)))
                   (cons n1 (cons n2 (nodes-of (cdr G))))))))
    

    In your case you will use add-to-set where I’ve used cons. Once we’ve reached the base case, all of the calls to add-to-set will be able to be evaluated starting with the last one then working back down the stack to the first.

    |cons n3 '()           | 2    => (n3)
    |cons n2 [result of 2] | 1    => (n2 n3)
    |cons n1 [result of 1] | 0    => (n1 n2 n3)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.