title: Scheme: Counting Structures
body: <pre><code>(count-by-type
  (list
  (make-animal "Slytherin" "snake" (make-date 2013 8 23))
  (make-animal "Toby" "dog" (make-date 2014 3 20))
  (make-animal "Curly" "dog" (make-date 2014 1 18))
  (make-animal "Maximus" "cat" (make-date 2013 10 7))
  (make-animal "Mia" "cat" (make-date 2013 10 7))))
  =&gt; (list (list 2 "cat") (list 2 "dog") (list 1 "snake"))
  
  ;; A Date is a structure (make-date y m d), where
  ;; y is positive integer (year),
  ;; m is an integer between 1 and 12 (month),
  ;; d is an integer between 1 and 31 (day of the month).
  (define-struct date (year month day))
  ;; An Animal is a structure (make-structure n t a), where
  ;; n is a nonempty string (name of animal),
  ;; t is a nonempty string (type of animal),
  ;; a is a Date (date animal arrived at the shelter).
  (define-struct animal (name type arrival))
  </code></pre>
  
  <p>Note: The applicable data structures as at the top.</p>
  
  <p>So I am trying to make a function (in Scheme using Dr. Racket) that consumes a list of Animal structures and produces a list of lists.</p>
  
  <p>The elements in the produced lists are of the form (list number type) where type is the type of animal and number is the number of that animal type in the consumed list.</p>
  
  <p>This much is doable for me, but here is where the issues comes in: -> The list needs to be in descending order -> Even worse, in the event of a tie between any number of pairs of lists, alphabetical sorting is utilized.</p>
  
  <p>Note: I wish to only solve this using abstract list functions excluding build-list and lambda (as I wish to strengthen my ability to solve such problems without advanced abstract functions)</p>
  
  <p>Keeping that in mind, I know this problem will use the abstract functions map, filter, and foldr, but can't quite think of how.</p>
  
  <p>Any help is appreciated.</p>