Guest User

Untitled

a guest
Feb 21st, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.68 KB | None | 0 0
  1. ;;; family.ss
  2. ;;; This programme implements the family functionality described in the project
  3. ;;; Govind Ramabadran
  4.  
  5. ;;; Function: New
  6. ;;; Arguments: None
  7. ;;; Effect: Clears the database
  8. (define new (lambda () '( () () )) )
  9.  
  10. ;;; Function: AddChild
  11. ;;; Arguments: Family Parents Individual
  12. ;;; Effect: Adds a new child to the database. If this child already exists in the database,
  13. ;;; AddChild should have no effect. First check to see if the child exists and return
  14. ;;; the family list if so, else cons the parent to the child to the list (which would
  15. ;;; be empty), apply lists around the new lists and combine them back into one.
  16. (define AddChild (lambda (Family Parents Individual) (if (ChildExists (cadr Family) Individual) Family
  17. (cons (car Family) (cons (cons (cons Parents (cons Individual ())) (cadr Family)) ())))))
  18.  
  19. ;;; Function: AddCouple
  20. ;;; Arguments: Family Spouse1 Spouse2
  21. ;;; Effect: Adds a new couple to the database. Create the list that contains the couple, then
  22. ;;; cons it to the existing family's Parents side and return back the family
  23. (define AddCouple (lambda (Family Spouse1 Spouse2) (cons (cons (cons Spouse1 (cons Spouse2
  24. ())) (car Family)) (cdr Family))))
  25.  
  26. ;;; Function: Children
  27. ;;; Arguments: Family Parents
  28. ;;; Effect: Returns a list of children of this individual. If the Parents does not exist in
  29. ;;; the database, or the Parents does not have children, return an empty list. Here,
  30. ;;; you'll get the spouse of the individual. Use GetChildrenList (later on) to
  31. ;;; recursively search for children with the spouse result and return that value.
  32. (define Children (lambda (Family Parents) (GetChildrenList (cadr Family) (GetParentList (car
  33. Family) Parents))))
  34.  
  35. ;;; Function: Parents
  36. ;;; Arguments: Family Individual
  37. ;;; Effect: Returns a list of the parents of the individual. If the individual does not exist
  38. ;;; in the database, or the Parents has no children, it returns an empty list.
  39. (define Parents (lambda (Family Individual) (GetParentList (car Family) (car (GetParent
  40. (cadr Family) Individual)))))
  41.  
  42. ;;; Function: Siblings
  43. ;;; Arguments: Family Individual
  44. ;;; Effect: Returns a list of the siblings of the individual. If the individual does not
  45. ;;; exist in the database, or has no siblings, it returns an empty list. Get the
  46. ;;; children of the Parents of the individual, removing the child themselves from
  47. ;;; this list.
  48. (define Siblings (lambda (Family Individual) (RemoveChild (GetChildrenList (cadr Family)
  49. (Parents Family Individual)) Individual)))
  50.  
  51. ;;; Function: Descendants
  52. ;;; Arguments: Family Indivdual
  53. ;;; Effect: Returns a list of all the children, grandchildren, great grandchildren, etc. of
  54. ;;; the individual. If the individual does not exist in the database, or has no
  55. ;;; offspring, it returns an empty list. This will have a recursive loop, so cond is
  56. ;;; is being used for the separate cases.
  57. ;;; Case 1: If the list is null, a null list is returned
  58. ;;; Case 2: If the current element is a list, append to it the appended results of
  59. ;;; Descendants with the pass of the car of the list and Descendants with the
  60. ;;; pass of the the cdr of the list
  61. ;;; Case 3: Otherwise, return the descendants of the children of the individual
  62. (define Descendants (lambda (Family Individual) (cond ((null? Individual) ()) ((Family?
  63. Individual) (append (append (Descendants Family (car Individual)) (Descendants Family
  64. (cdr Individual))) (cons (car Individual) ()))) (else (Descendants Family (Children
  65. Family Individual))))))
  66.  
  67. ;;; Function: ChildExists
  68. ;;; Arguments: Family Individual
  69. ;;; Effect: Returns whether or not the individual is in the family. This will have a
  70. ;;; recursive loop, so cond is being used for the separate cases.
  71. ;;; Case 1: If the list is empty, return false
  72. ;;; Case 2: If the car of the list is (), the first addition in the list, return false
  73. ;;; Case 3: If the cdr of the car of the list is the individual, return true
  74. ;;; Case 4: Otherwise, call ChildExists on the cdr of the list, passing the list
  75. ;;; and the name of the child
  76. (define ChildExists (lambda (Family Individual) (cond ((eqv? () Family) #f) ((eqv? () (car
  77. Family)) #f) ((eqv? (cadar Family) Individual) #t) (else (ChildExists (cdr Family)
  78. Individual)))))
  79.  
  80. ;;; Function: GetChildren
  81. ;;; Arguments: Family Individual
  82. ;;; Effect: Returns the children of the Parents passed in. This will have a recursive loop, so
  83. ;;; cond is being used for the separate cases.
  84. ;;; Case 1: If the list is null, return a null list
  85. ;;; Case 2: If the car of the car of the list is a Parents, append the cdr of the car
  86. ;;; to the result of GetChildren, which should be sent the cdr of the list as
  87. ;;; well as the Parents.
  88. ;;; Case 3: Otherwise, return the result of GetChildren, passing the cdr of the Family
  89. ;;; and the Individual
  90. (define GetChildren (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (caar Family
  91. Individual) (append (cdar Family) (GetChildren (cdr Family) Individual))) (else
  92. (GetChildren (cdr Family) Individual))))))
  93.  
  94. ;;; Function: GetChildrenList
  95. ;;; Arguments: Family Parents
  96. ;;; Effect: Returns the children of the parents in question. This will have a recursive loop,
  97. ;;; so if is being used for the single case. If the Parents list is null, return an
  98. ;;; empty list. Otherwise, cons the result of cdr of the Parents in GetChildrenList to
  99. ;;; the result of GetChildren, passing the car of the Parents
  100. (define GetChildrenList (lambda (Family Parents) (if (null? Parents) () (append
  101. (GetChildrenList Family (cdr Parents)) (GetChildrenList Family (car Parents))))))
  102.  
  103. ;;; Function: GetParent
  104. ;;; Arguments: Family Individual
  105. ;;; Effect: Returns either a list containing the parent desired or an empty list. This will
  106. ;;; have a recursive loop, so cond is being used for the separate cases.
  107. ;;; Case 1: If the list is null, return an empty list
  108. ;;; Case 2: If the cdr of the car of the Family is the Individual, cons the car of
  109. ;;; the car to an empty list and return it
  110. ;;; Case 3: Otherwise, return GetParent, sending the cdr of the Family and the
  111. ;;; Individual being searched for
  112. (define GetParent (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (cadar Family)
  113. Individual) (cons (caar Family ())) (else (GetParent (cdr Family) Individual))))))
  114.  
  115. ;;; Function: GetParentList
  116. ;;; Arguments: Family Parents
  117. ;;; Effect: Returns the spouses of the Parents in question. This will have a recursive loop,
  118. ;;; so cond is being used for the separate cases.
  119. ;;; Case 1: If the list is null, return a null list.
  120. ;;; Case 2: If the car of the car of the Family is a Parent, append the cdr of the car
  121. ;;; to the result of GetParentList, which should be sent the cdr of the Family
  122. ;;; as well as the Parent
  123. ;;; Case 3: If the cdr of the car of the Family is a Parent, append the car of the car
  124. ;;; to the result of GetParentList, which should be sent the cdr of the Family
  125. ;;; as well as the Parent
  126. ;;; Case 4: Otherwise, return the result of GetParentList, passing the cdr of Family
  127. ;;; and Parent
  128. (define GetParentList (lambda (Family Parents) (cond ((null? Family) (cons Parents ())) ((eqv?
  129. (caar Family) Parents) (cons (cadar Family) (GetParentList (cdr Family) Parents)))
  130. ((eqv? (cadar Family) Parents) (cons (caar Family) (GetParentList (cdr Family)
  131. Parents))) (else (GetParentList (cdr Family) Parents)))))
  132.  
  133. ;;; Function: GetSpouse
  134. ;;; Args: Family Individual
  135. ;;; Effect: Returns either a list containing the marriage of a person and the spouse or
  136. ;;; an empty list. This will have a recursive loop, so cond is being used for the
  137. ;;; separate cases.
  138. ;;; Case 1: If the list is null, return an empty list.
  139. ;;; Case 2: If the car of the car of the Family is the Individual, cons the cdr of the
  140. ;;; car to the Individual to an empty list and return it.
  141. ;;; Case 3: If the cdr of the car of the Family is the Individual, cons the car of the
  142. ;;; car to the Individual to an empty list and return it.
  143. ;;; Case 4: Otherwise, return GetSpouse, sending the cdr of the Family and the
  144. ;;; Individual being searched for.
  145. (define GetSpouse (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (caar Family)
  146. Individual) (cons (cadar Family) (cons Individual ()))) ((eqv? (cadar Family)
  147. Individual) (cons (caar Family) (cons Individual ()))) (else (GetSpouse (cdr Family)
  148. Individual)))))
  149.  
  150. ;;; Function: RemoveChild
  151. ;;; Args: Family Child
  152. ;;; Effect: Returns a list with the element specified removed. This will have a recursive
  153. ;;; loop, so cond is being used for the separate cases.
  154. ;;; Case 1: If the list is null, return an empty list
  155. ;;; Case 2: If the car of the Family is the Individual, return the cdr of the Family
  156. ;;; Case 3: Otherwise, add the car of the Family to the result of RemoveChild, passing
  157. ;;; it the cdr of the Family and the Individual's name
  158. (define RemoveChild (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (car Family)
  159. Individual) (cdr Family)) (else (append (car Family) (RemoveChild (cdr Family) Individual))))))
Add Comment
Please, Sign In to add comment