views:

59

answers:

1

So 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>
+2  A: 

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.

Adam Rosenfield
Thanks for the fast reply. I realized the recursion was off, just couldn't figure out how to fix it. Managed to get it working, thanks to your help. Again, thank you!
Unknown50862