谁能给我一些关于这个问题的提示(家谱)?

发布于 2024-12-05 04:45:01 字数 860 浏览 1 评论 0原文

它来自我的家庭作业。有一个家谱

                             a  +  b
                         /   |    |   \ 
                        c+u  d+c  e+w  f
                     / | \        / \
                 m+x  n+y  o      p  q
                  |
                  r

a 和 b 是最古老的。而每个已婚人士的第二个人都不是原生家庭的一部分。 现在我需要编写配偶、兄弟姐妹、孩子、孙子、父母和祖父母函数。

我写的清单如下: ( (father mother) chlid1 child2 child3)

(((a b) c d e f) ((c u) m n o) ((d v) nil) ((e w) p q) (f nil) ((m x) r) ((n y) nil) (o nil) (p nil) (q nil)  )

我的兄弟函数有一些问题,这是我的代码。

(defun sibling  (arglst lst)  
 (cond
        ((eql 
             arglst (cdr (car lst))) 
                 (rest (cdr lst))
         )
   (T (sibling (rest lst) arglst))

)

我知道这是错误的,但我不知道如何修改它..而且我还需要其他功能的帮助。希望能从你们那里得到一些提示。

It comes from my homework assignments. There is a family tree

                             a  +  b
                         /   |    |   \ 
                        c+u  d+c  e+w  f
                     / | \        / \
                 m+x  n+y  o      p  q
                  |
                  r

a and b is the oldest. and every married people the second person is not part of the original family.
Now I need to write the spouse, sibling, children ,grandchildren, parents and grandparents function.

I wrote the list as below:
( (father mother) chlid1 child2 child3)

(((a b) c d e f) ((c u) m n o) ((d v) nil) ((e w) p q) (f nil) ((m x) r) ((n y) nil) (o nil) (p nil) (q nil)  )

I have some problems with sibling function, here is my code.

(defun sibling  (arglst lst)  
 (cond
        ((eql 
             arglst (cdr (car lst))) 
                 (rest (cdr lst))
         )
   (T (sibling (rest lst) arglst))

)

I knew it was wrong, but I don't how to revise it.. and I also need some help with other functions. hope can get some hints from u guys.

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

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

发布评论

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

评论(2

凉世弥音 2024-12-12 04:45:01

由于这是家庭作业,我不会给出完整的解决方案,但这应该足以让您解决其余问题:

(defparameter *family* '(((a b) c d e f)
                         ((c u) m n o)
                         ((d v) nil)
                         ((e w) p q)
                         (f nil)
                         ((m x) r)
                         ((n y) nil)
                         (o nil)
                         (p nil)
                         (q nil)))

(defun siblings (person family)
  "Return a list of PERSON's siblings."
  (remove person (cdr (find person family :key #'cdr :test #'member))))

(defun siblingsp (person1 person2 family)
  "Are PERSON1 and PERSON2 siblings?"
  (find person2 (siblings person1 family)))

(defun parents (person family)
  "Return a list of PERSON's parents."
  (car (find person family :key #'cdr :test #'member)))

(defun parentp (parent child family)
  "Is PARENT a parent of CHILD?"
  (find parent (parents child family)))

尝试一下:

CL-USER> (siblings 'p *family*)
(Q)
CL-USER> (siblingsp 'q 'p *family*)
P
CL-USER> (parents 'p *family*)
(E W)

例如,现在要找到祖父母,您只需了解祖父母是什么:​​( ) 父母双方的父母。然后,问问自己,孙子们过得怎么样。最后,鉴于此示例,spouse 函数应该相当简单。

Since this is homework, I won't give the full solution, but this should suffice for you to solve the rest:

(defparameter *family* '(((a b) c d e f)
                         ((c u) m n o)
                         ((d v) nil)
                         ((e w) p q)
                         (f nil)
                         ((m x) r)
                         ((n y) nil)
                         (o nil)
                         (p nil)
                         (q nil)))

(defun siblings (person family)
  "Return a list of PERSON's siblings."
  (remove person (cdr (find person family :key #'cdr :test #'member))))

(defun siblingsp (person1 person2 family)
  "Are PERSON1 and PERSON2 siblings?"
  (find person2 (siblings person1 family)))

(defun parents (person family)
  "Return a list of PERSON's parents."
  (car (find person family :key #'cdr :test #'member)))

(defun parentp (parent child family)
  "Is PARENT a parent of CHILD?"
  (find parent (parents child family)))

Try it:

CL-USER> (siblings 'p *family*)
(Q)
CL-USER> (siblingsp 'q 'p *family*)
P
CL-USER> (parents 'p *family*)
(E W)

Now, to find the grandparents for example, you just have to understand what grandparents are: (A list of) The parents of both parents. Then, ask yourself how it is for grandchildren. Finally, the spouse function should be rather easy, given this example.

丑疤怪 2024-12-12 04:45:01

例如,我不知道为什么它只能返回第一级和第二级。 a 返回 b,c 返回 u 。但是当我输入 m 时,它返回一个错误:

- MEMBER:正确的列表不得以 F 结尾

我检查了代码,没有发现任何问题。为什么它不能搜索第三层?但它可以找到孙子,所以我想如果它可以做孙子搜索为什么它不能做配偶搜索? Member 函数有问题吗?无论如何,您的代码非常简单且易于阅读和理解。多谢。

(defun spouse ( family-tree2 person) ;Find person's spouse
   (remove person (car (find person family-tree2 :key #'car :test #'member)))
);end

I don't know why it can only return first level and second level,for example. a returns b, c returns u . but when I entere m ,it returns an erro :

- MEMBER: A proper list must not end with F.

I checked the code and didn't find any problems. why it cannot search the third level? but it can find grandchildren,so I think if it can do grandchildren search why it cannot do spouse search? is there something wrong with Member function? anyway, your code is really simple and easy to read and understand. Thanks a lot.

(defun spouse ( family-tree2 person) ;Find person's spouse
   (remove person (car (find person family-tree2 :key #'car :test #'member)))
);end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文