More rival invlist list implementation 

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 Some .scm files for playing around with things (Mark Kittisopikul)
Date: 2002, Dec 07
From: Ivan Beschastnikh <ivan@uchicago.edu>

decided to write my own; now looking at Mark's it seems i have made mine a tab bit lengthy but the random list generator was too good to pass up.

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

(define (members_invlist il)
  ;returns normal Scheme list containing all of the symbols appearing in il
  ;no guarantee what order the symbols appear in, but each appears exactly once.

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

  (define (construct_scheme_list made_list remaining_il)
    (cond ((null? remaining_il) made_list)
	  ((contains? made_list (car remaining_il))
	   (construct_scheme_list made_list (cdr remaining_il)))
	  (else
	   (construct_scheme_list (cons (car remaining_il) made_list) (cdr remaining_il)))))

(define (get_random_list listik n) ;creates a non-repeated list of random integers btw/ 0 and n

    (define (new_num_gen)
      (let ((new_rand (random n)))
	(if (contains? listik new_rand)
       ;if listik contains new_rand then must randomize again
	    (new_num_gen)
	  new_rand)))

    (cond ((eq? (length listik) n) listik)
	  (else
	   (get_random_list (cons (new_num_gen) listik) n))))

  (define (get_elem listin location)
    ;first element is indexed as 0!
    (if (eq? location 0) (car listin)
      (get_elem (cdr listin) (- location 1))))

  (define (randomize_elem_locations listin)
    (let ((rand_places_list (get_random_list () (length listin))))
      (map get_elem (make-list (length listin) listin) rand_places_list)))

  (randomize_elem_locations (construct_scheme_list () il)))

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

(define (nbr_appearances_invlist s il)

  (define (counter n list)
    (cond ((null? list) n)
	  ((eq? (car list) s) (counter (+ n 1) (cdr list)))
	  (else
	   (counter n (cdr list)))))

  (counter 0 il))

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

(define (locations_invlist s il)

  (define (locations_getter working_list locations_list n)
    (cond ((null? working_list) locations_list)
	  ((eq? (car working_list) s) (locations_getter (cdr working_list) (append locations_list (list n)) (+ n 1)))
	  (else
	   (locations_getter (cdr working_list) locations_list (+ n 1)))))

  (cond ((eq? (nbr_appearances_invlist s il) 0) ())
	(else
	 (locations_getter il () 1))))


Messages

1. Feedback Why rival? ;-) by Mark Kittisopikul, 2002, Dec 07

to: "rival invlist list implementation"