排序问题

发布于 2024-09-29 11:23:36 字数 2621 浏览 4 评论 0原文

我正在课堂上学习Scheme,我的教授在8:00之后就不回答问题,所以我希望大家能帮助我。基本上我有一个家谱类型的东西,我试图获取一个人的所有祖先,并将它们显示为一个字符串,按字母顺序排序。

问题是,由于递归,每一代都被组合成自己的字符串,所以我得到的是“人a人b”,而不是“人a”“人b”。因此,当我对它们进行排序时,在将它们全部附加到一个字符串之前,它只会对这对中的第一个名称进行排序,这意味着第二个名称不会被排序。

抱歉,如果这听起来令人困惑,我自己也不知道如何解释。我希望代码能够为我解释大部分内容。

(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")

检查故障:

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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2024-10-06 11:23:36

正如您所说,问题是在递归情况下,祖先返回包含所有祖先的单个字符串,然后无法正确排序,因为它是一件事,而不是几件事。

您应该修改ancestors,以便它返回一个字符串列表,而不是返回单个字符串(即不要在其中调用strlist-to-str)。然后,当它递归时,你得到母亲的祖先列表和父亲的祖先列表;将这些列表以及母亲和父亲添加在一起,并将它们合并为一个列表。

仅作为最后一步,在所有递归完成后,您才应调用 strlist-to-str 将最终的名称列表合并为单个字符串。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文