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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:11:14+00:00 2026-05-17T21:11:14+00:00

I’m learning Scheme in a class and my professor doesn’t answer questions after 8:00,

  • 0

I’m learning Scheme in a class and my professor doesn’t answer questions after 8:00, so I’m hoping you all can help me out. Basically I have a family tree type thing and I’m trying to get all the ancestors of one person, and display them as one string, sorted alphabetically.

The problem is, because of the recursion, each generation is being combined into their own string, so instead of getting “person a” “person b”, I’m getting “person a person b”. So when I go to sort them, before appending them all to one string, it only sorts the first name in the pair meaning that the second name doesn’t get sorted.

Sorry if that sounds confusing, I’m not exactly sure how to explain it myself. I’m hoping that the code will explain most of it for me.

(define-struct person  
  (
  first    ; a string: first name
  last    ; a string: last name
  sex     ; a symbol: 'male, 'female
  eyes     ; a symbol: 'blue, 'brown', 'green
  hair     ; a symbol: 'blonde, 'brown, 'black, 'red
  mother; a person: empty if not known
  father; a person: empty if not known
  born    ; a number: year of birth
  )
)

(define P-00000 (make-person "Alexandra" "Harper" 'female 'blue 'red empty empty 1897))
(define P-10000 (make-person "Joshua" "Sherman" 'male 'green 'blonde empty empty 1881))
(define P-20000 (make-person "Alexandra" "Hazel" 'female 'brown 'red empty empty 1906))
(define P-30000 (make-person "Christopher" "Abdul" 'male 'brown 'brown empty empty 1904))
(define P-01000 (make-person "Lauren" "Sherman" 'female 'green 'black P-00000 P-10000 1914))
(define P-21000 (make-person "Alexander" "Abdul" 'male 'blue 'brown P-20000 P-30000 1927))
(define P-01100 (make-person "Justine" "Abdul" 'female 'blue 'black P-01000 P-21000 1949))

(define (strlist-to-str StrLst Sep)
   (cond
      [(empty? StrLst) ""]
      [(equal? (first StrLst) "")  (strlist-to-str (rest StrLst) Sep)]
      [(empty? (rest StrLst)) (first StrLst)]
      [else (string-append (first StrLst) Sep (strlist-to-str (rest StrLst) Sep))]
   )
)

(define (person-to-lfn ; string
   who                 ; person
   )
   (cond
     [(string? who)  who]
     [else  (string-append (person-last who) "," (person-first who))]
   )
)

(define (ancestors ; string
   who             ; person
   )
   (cond
     [(empty? (person-mother who))  ""]
     [else  
      (strlist-to-str (sort (list (person-to-lfn (person-mother who))
                                  (person-to-lfn (person-father who))
                                  (ancestors (person-mother who))
                                  (ancestors (person-father who))) string<?) " ")]
   )
)
(check-expect (ancestors P-01100) "Abdul,Alexander Abdul,Christopher Harper,Alexandra Hazel,Alexandra Sherman,Joshua Sherman,Lauren")

Check failures:

Actual value "Abdul,Alexander Abdul,Christopher Hazel,Alexandra Harper,Alexandra Sherman,Joshua Sherman,Lauren" differs from "Abdul,Alexander Abdul,Christopher Harper,Alexandra Hazel,Alexandra Sherman,Joshua Sherman,Lauren", the expected value.
at line 64, column 0 <code>
  • 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-17T21:11:15+00:00Added an answer on May 17, 2026 at 9:11 pm

    The issue, as you said, is that in the recursive case, ancestors is returning a single string containing all of the ancestors, which then can’t be sorted properly because it’s one thing, not several things.

    You should modify ancestors so that instead of returning a single string, it returns a list of strings (i.e. don’t call strlist-to-str in it). Then, when it recurses, you get the list of the mother’s ancestors and the list of the father’s ancestors; add these lists together, along with the mother and father, and flatten them into a single list.

    Only as the final step, after all of the recursion has finished, should you call strlist-to-str to combine the final list of names into a single string.

    • 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
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I have text I am displaying in SIlverlight that is coming from a CMS
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
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 want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

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.