Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;; family.ss
- ;;; This programme implements the family functionality described in the project
- ;;; Govind Ramabadran
- ;;; Function: New
- ;;; Arguments: None
- ;;; Effect: Clears the database
- (define new (lambda () '( () () )) )
- ;;; Function: AddChild
- ;;; Arguments: Family Parents Individual
- ;;; Effect: Adds a new child to the database. If this child already exists in the database,
- ;;; AddChild should have no effect. First check to see if the child exists and return
- ;;; the family list if so, else cons the parent to the child to the list (which would
- ;;; be empty), apply lists around the new lists and combine them back into one.
- (define AddChild (lambda (Family Parents Individual) (if (ChildExists (cadr Family) Individual) Family
- (cons (car Family) (cons (cons (cons Parents (cons Individual ())) (cadr Family)) ())))))
- ;;; Function: AddCouple
- ;;; Arguments: Family Spouse1 Spouse2
- ;;; Effect: Adds a new couple to the database. Create the list that contains the couple, then
- ;;; cons it to the existing family's Parents side and return back the family
- (define AddCouple (lambda (Family Spouse1 Spouse2) (cons (cons (cons Spouse1 (cons Spouse2
- ())) (car Family)) (cdr Family))))
- ;;; Function: Children
- ;;; Arguments: Family Parents
- ;;; Effect: Returns a list of children of this individual. If the Parents does not exist in
- ;;; the database, or the Parents does not have children, return an empty list. Here,
- ;;; you'll get the spouse of the individual. Use GetChildrenList (later on) to
- ;;; recursively search for children with the spouse result and return that value.
- (define Children (lambda (Family Parents) (GetChildrenList (cadr Family) (GetParentList (car
- Family) Parents))))
- ;;; Function: Parents
- ;;; Arguments: Family Individual
- ;;; Effect: Returns a list of the parents of the individual. If the individual does not exist
- ;;; in the database, or the Parents has no children, it returns an empty list.
- (define Parents (lambda (Family Individual) (GetParentList (car Family) (car (GetParent
- (cadr Family) Individual)))))
- ;;; Function: Siblings
- ;;; Arguments: Family Individual
- ;;; Effect: Returns a list of the siblings of the individual. If the individual does not
- ;;; exist in the database, or has no siblings, it returns an empty list. Get the
- ;;; children of the Parents of the individual, removing the child themselves from
- ;;; this list.
- (define Siblings (lambda (Family Individual) (RemoveChild (GetChildrenList (cadr Family)
- (Parents Family Individual)) Individual)))
- ;;; Function: Descendants
- ;;; Arguments: Family Indivdual
- ;;; Effect: Returns a list of all the children, grandchildren, great grandchildren, etc. of
- ;;; the individual. If the individual does not exist in the database, or has no
- ;;; offspring, it returns an empty list. This will have a recursive loop, so cond is
- ;;; is being used for the separate cases.
- ;;; Case 1: If the list is null, a null list is returned
- ;;; Case 2: If the current element is a list, append to it the appended results of
- ;;; Descendants with the pass of the car of the list and Descendants with the
- ;;; pass of the the cdr of the list
- ;;; Case 3: Otherwise, return the descendants of the children of the individual
- (define Descendants (lambda (Family Individual) (cond ((null? Individual) ()) ((Family?
- Individual) (append (append (Descendants Family (car Individual)) (Descendants Family
- (cdr Individual))) (cons (car Individual) ()))) (else (Descendants Family (Children
- Family Individual))))))
- ;;; Function: ChildExists
- ;;; Arguments: Family Individual
- ;;; Effect: Returns whether or not the individual is in the family. This will have a
- ;;; recursive loop, so cond is being used for the separate cases.
- ;;; Case 1: If the list is empty, return false
- ;;; Case 2: If the car of the list is (), the first addition in the list, return false
- ;;; Case 3: If the cdr of the car of the list is the individual, return true
- ;;; Case 4: Otherwise, call ChildExists on the cdr of the list, passing the list
- ;;; and the name of the child
- (define ChildExists (lambda (Family Individual) (cond ((eqv? () Family) #f) ((eqv? () (car
- Family)) #f) ((eqv? (cadar Family) Individual) #t) (else (ChildExists (cdr Family)
- Individual)))))
- ;;; Function: GetChildren
- ;;; Arguments: Family Individual
- ;;; Effect: Returns the children of the Parents passed in. This will have a recursive loop, so
- ;;; cond is being used for the separate cases.
- ;;; Case 1: If the list is null, return a null list
- ;;; Case 2: If the car of the car of the list is a Parents, append the cdr of the car
- ;;; to the result of GetChildren, which should be sent the cdr of the list as
- ;;; well as the Parents.
- ;;; Case 3: Otherwise, return the result of GetChildren, passing the cdr of the Family
- ;;; and the Individual
- (define GetChildren (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (caar Family
- Individual) (append (cdar Family) (GetChildren (cdr Family) Individual))) (else
- (GetChildren (cdr Family) Individual))))))
- ;;; Function: GetChildrenList
- ;;; Arguments: Family Parents
- ;;; Effect: Returns the children of the parents in question. This will have a recursive loop,
- ;;; so if is being used for the single case. If the Parents list is null, return an
- ;;; empty list. Otherwise, cons the result of cdr of the Parents in GetChildrenList to
- ;;; the result of GetChildren, passing the car of the Parents
- (define GetChildrenList (lambda (Family Parents) (if (null? Parents) () (append
- (GetChildrenList Family (cdr Parents)) (GetChildrenList Family (car Parents))))))
- ;;; Function: GetParent
- ;;; Arguments: Family Individual
- ;;; Effect: Returns either a list containing the parent desired or an empty list. This will
- ;;; have a recursive loop, so cond is being used for the separate cases.
- ;;; Case 1: If the list is null, return an empty list
- ;;; Case 2: If the cdr of the car of the Family is the Individual, cons the car of
- ;;; the car to an empty list and return it
- ;;; Case 3: Otherwise, return GetParent, sending the cdr of the Family and the
- ;;; Individual being searched for
- (define GetParent (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (cadar Family)
- Individual) (cons (caar Family ())) (else (GetParent (cdr Family) Individual))))))
- ;;; Function: GetParentList
- ;;; Arguments: Family Parents
- ;;; Effect: Returns the spouses of the Parents in question. This will have a recursive loop,
- ;;; so cond is being used for the separate cases.
- ;;; Case 1: If the list is null, return a null list.
- ;;; Case 2: If the car of the car of the Family is a Parent, append the cdr of the car
- ;;; to the result of GetParentList, which should be sent the cdr of the Family
- ;;; as well as the Parent
- ;;; Case 3: If the cdr of the car of the Family is a Parent, append the car of the car
- ;;; to the result of GetParentList, which should be sent the cdr of the Family
- ;;; as well as the Parent
- ;;; Case 4: Otherwise, return the result of GetParentList, passing the cdr of Family
- ;;; and Parent
- (define GetParentList (lambda (Family Parents) (cond ((null? Family) (cons Parents ())) ((eqv?
- (caar Family) Parents) (cons (cadar Family) (GetParentList (cdr Family) Parents)))
- ((eqv? (cadar Family) Parents) (cons (caar Family) (GetParentList (cdr Family)
- Parents))) (else (GetParentList (cdr Family) Parents)))))
- ;;; Function: GetSpouse
- ;;; Args: Family Individual
- ;;; Effect: Returns either a list containing the marriage of a person and the spouse or
- ;;; an empty list. This will have a recursive loop, so cond is being used for the
- ;;; separate cases.
- ;;; Case 1: If the list is null, return an empty list.
- ;;; Case 2: If the car of the car of the Family is the Individual, cons the cdr of the
- ;;; car to the Individual to an empty list and return it.
- ;;; Case 3: If the cdr of the car of the Family is the Individual, cons the car of the
- ;;; car to the Individual to an empty list and return it.
- ;;; Case 4: Otherwise, return GetSpouse, sending the cdr of the Family and the
- ;;; Individual being searched for.
- (define GetSpouse (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (caar Family)
- Individual) (cons (cadar Family) (cons Individual ()))) ((eqv? (cadar Family)
- Individual) (cons (caar Family) (cons Individual ()))) (else (GetSpouse (cdr Family)
- Individual)))))
- ;;; Function: RemoveChild
- ;;; Args: Family Child
- ;;; Effect: Returns a list with the element specified removed. This will have a recursive
- ;;; loop, so cond is being used for the separate cases.
- ;;; Case 1: If the list is null, return an empty list
- ;;; Case 2: If the car of the Family is the Individual, return the cdr of the Family
- ;;; Case 3: Otherwise, add the car of the Family to the result of RemoveChild, passing
- ;;; it the cdr of the Family and the Individual's name
- (define RemoveChild (lambda (Family Individual) (cond ((null? Family) ()) ((eqv? (car Family)
- Individual) (cdr Family)) (else (append (car Family) (RemoveChild (cdr Family) Individual))))))
Add Comment
Please, Sign In to add comment