Reputation: 623
For reference, I am programming with Scheme
using DrRacket
.
I am trying to make the function tally-by-all
which consumes a list-of-candidates
and list-of-votes
to output a list-of-voting-tallies
with the help of the helper function that is already defined, top-votes-for
.
When I run it as is, an error occurs because the helper function top-votes-for
consumes a string
and list-of-votes
so it can't exactly consume a list-of-strings
in place of a string
, at least I think so anyways.
The output I am supposed to get is:
(cons (make-voting-tally "Blake" 2)
(cons (make-voting-tally "Ash" 0)
(cons (make-voting-tally "Bob" 1)
(cons (make-voting-tally "Will" 0)
(cons (make-voting-tally "Joey" 0) empty))))))
Keep in mind that everything in the program is correct except for the definition of tally-by-all
.
Any assistance would be greatly appreciated.
Thank you in advance!
Here is my program:
;; Data Definition
(define-struct vote (choice1 choice2 choice3))
;; A vote is a structure: (make-vote String String String).
;; interp. 3 candidates that one person has voted for (String).
(define vote1
(make-vote "Blake" "Joey" "Will"))
(define vote2
(make-vote "Blake" "Bob" "Ash"))
(define vote3
(make-vote "Bob" "Ash" "Blake"))
(define listofVotes
(list vote1 vote2 vote3))
;; Signature: top-votes-for: string list-of-strings -> number
;; Purpose: Consumes a name and a list of votes and produces the number of
;; times that the given name was the first choice vote in the list of votes.
;; (This tallies points under winner-takes-all strategy.)
;; Tests:
(check-expect (top-votes-for "Blake" empty) 0)
(check-expect (top-votes-for "Blake" listofVotes) 2)
(check-expect (top-votes-for "Bob" listofVotes) 1)
(check-expect (top-votes-for "Ash" listofVotes) 0)
(check-expect (top-votes-for "Joey" listofVotes) 0)
(check-expect (top-votes-for "Will" listofVotes) 0)
;; Define:
(define (top-votes-for cand alov)
(cond
[(empty? alov) 0]
[(string=? (vote-choice1 (first alov)) cand) (+ 1 (top-votes-for cand (rest alov)))]
[else (top-votes-for cand (rest alov))]
)
)
;; Data Definition
(define-struct voting-tally (candidate numVotes))
;; A voting-tally is a structure: (make-voting-tally String Number).
;; interp. a candidate (String) and how many votes said
;; candidate has gotten (Number).
(define listofCandidates
(list "Blake" "Ash" "Bob" "Will" "Joey"))
;; Signature: tally-by-all: list-of-canidates list-of-votes -> list-of-Voting-Tallies
;; Purpose: Consumes a list of candidate names and a list of votes and produces a
;; list of voting-tallies.
;; Tests:
(check-expect (tally-by-all empty empty) empty)
(check-expect (tally-by-all listofCandidates listofVotes) (cons (make-voting-tally "Blake" 2)
(cons (make-voting-tally "Ash" 0)
(cons (make-voting-tally "Bob" 1)
(cons (make-voting-tally "Will" 0)
(cons (make-voting-tally "Joey" 0)
empty))))))
;; Define:
(define (tally-by-all aloc alov)
(cond
[(empty? alov) empty]
[else (cons (make-voting-tally (first aloc) (tally-by-all alov (rest (top-votes-for aloc alov)))))]
)
)
Upvotes: 0
Views: 134
Reputation: 49920
I think what you want is to recurse over the list of candidates (aloc
), and for each one, compute his/her tally from all of the votes:
(define (tally-by-all aloc alov)
(cond
[(empty? aloc) empty]
[else (cons (make-voting-tally (first aloc) (top-votes-for (first aloc) alov))
(tally-by-all (rest aloc) alov)))]
)
)
Upvotes: 1