More solutions to study questions 

Forum: Com Sci 12500, U Chicago, Autumn 2002, Discuss Classwork
Re: News I posted a study guide for the final (Mike O'Donnell)
Re: News Study questions for problem 1 (Mike O'Donnell)
Re: More Study questions for problem 3 (Mike O'Donnell)
Date: 2002, Dec 09
From: Ivan Beschastnikh <ivan@uchicago.edu>

i have the following solutions to Question3 Study questions. They seem super long though. Am i doing something wrong or are they really this voluminous?

;--------------------------------------- ;Find all occurrences of the contiguous sequence a b c in an invlist:

;find_seq returns a list of all starting points ;of contiguous sequence seq_list in invlist:

(define (find_seq seq_list invlist)

  (define (contains? elem list)
    (cond ((null? list) #f)
	  (else
	   (if (eq? elem (car list)) 
	       #t
	       (contains? elem (cdr list))))))

  (define (consecutive? location locations_list_to_check)
    (cond ((null? locations_list_to_check) #t)
	  (else
	   (if (contains? location (car locations_list_to_check))
	       (consecutive? (+ location 1) (cdr locations_list_to_check))
	       #f))))

  (let ((locations_list (map locations_invlist seq_list 
			     (make-list (length seq_list) invlist))))

    (define (seq locations_remaining result)
      (cond ((null? locations_remaining) result)
	    (else
	     (if (consecutive? (car locations_remaining) locations_list)
		 (seq (cdr locations_remaining) 
		      (append result (list (car locations_remaining))))
		 (seq (cdr locations_remaining) result)))))

    (warn "locations_list: " locations_list)
    ;have to find consecutive numbers within these lists

    (seq (car locations_list) (list))))

;-----------------------------------------------------------

;Determine whether the sequence a b c appears in an invlist ;, in that order, but not necessarily contiguously.:

(define (find_non_contiguous_seq list_seq invlist)

  (define (location_identifier min_location locations_list)
    ;searches through locations list and returns #t if a location
    ;was found that is greater than the min_location

    (cond ((null? locations_list) #f)
	  ((< min_location (car locations_list)) (car locations_list))
	  (else
	   (location_identifier min_location (cdr locations_list)))))

  (define (itter member_list_remaining min_location)
    (cond ((null? member_list_remaining) #t)
	  (else
	   (let ((locations (locations_invlist 
			     (car member_list_remaining) 
			     invlist)))
	     (cond ((null? locations) #f)
		   (else
		    (let ((location (location_identifier 
				     min_location
				     locations)))
		      (if (eq? location #f)
			  #f
			  (itter (cdr member_list_remaining) location)))))))))

  (cond ((null? list_seq) #f)
	(else 
	 (let ((locations (locations_invlist (car list_seq) invlist)))
	   (cond ((null? locations) #f)
		 (else
		  (itter (cdr list_seq) (car locations))))))))

;-----------------------------------------------------------

;Determine whether two invlists are equal: ;have to compare all elems and their respective locations

(define (invlist_eq? invlist1 invlist2)

    (define (member_compare member1)
      (let ((locations1 (locations_invlist member1 invlist1)) 
	    (locations2 (locations_invlist member1 invlist2)))
	(equal? locations1 locations2)))

    (define (compare_members members1_remaining)
      (if (null? members1_remaining) #t
	   (if (not (member_compare (car members1_remaining))) #f
	       (compare_members (cdr members1_remaining)))))

    (if (compare_members (members_invlist invlist1))
	(compare_members (members_invlist invlist2))
	#f))

;-----------------------------------------------------------

;test if palindrome:

(define (palindrome? invlist)

  (define (length_invlist members_remaining n)
    (cond ((null? members_remaining) n)
	  (else
	   (length_invlist (cdr members_remaining)
			   (+ n (length
				 (locations_invlist 
				  (car members_remaining) invlist))))
	   )))

  (define (member? listin member)
    (cond ((null? listin) #f)
	  ((eq? member (car listin)) #t)
	  (else (member? (cdr listin) member))))

  (let ((n (length_invlist (members_invlist invlist) 0)))

    (define (palindrome_locations? locations_list locations_testing)
      (warn "palindrome_locations?")
      (if (null? locations_testing) #t
	  (let ((middle (/ (+ n 1) 2.0)) (location (car locations_testing)))
	    (cond ((eq? location middle)
		   (palindrome_locations? locations_list (cdr locations_testing)))
		  ((< location middle) ;to the left of middle
		   (if (not (member? locations_list (- n (- location 1)))) #f
		       (palindrome_locations? locations_list (cdr locations_testing))))
		  ((> location middle) ;to the right of middle
		   (if (not (member? locations_list (+ 1 (- n location)))) #f
		       (palindrome_locations? locations_list (cdr locations_testing))))))))
         ;test for exact middle skipped b/c always ok to be in middle (no partner)

    (define (itter members)
      (warn "itter")
      (if (null? members) #t
	  (let ((member_locations (locations_invlist (car members) invlist)))
	    (if (not (palindrome_locations? member_locations member_locations))
		#f
		(itter (cdr members))))))

    (warn "started length:" n)
    (itter (members_invlist invlist))))


Messages

1. Feedback They can be shorter by Mike O'Donnell, 2002, Dec 09

to: "solutions to study questions"