简体   繁体   English

两个清单作为生产清单的输入-方案

[英]Two Lists as Input to Produce List - Scheme

For reference, I am programming with Scheme using DrRacket . 供参考,我正在使用DrRacket使用Scheme进行编程。

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 . 我正在尝试使功能全部tally-by-all该功能消耗list-of-candidates list-of-votes list-of-voting-tallies ,以借助已经定义的helper函数输出list-of-votes tally-by-all list-of-candidates 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. 当我按原样运行时,会发生错误,因为辅助函数top-votes-for消耗了一个string和一个list-of-votes因此它不能完全消耗一个list-of-strings来代替string ,至少我还是这样想。

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 . 请记住,程序中的所有内容都是正确的,除非定义了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)))))]
    )
  )

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: 我认为您想要的是对候选人列表( aloc )进行递归,并针对每个候选人,从所有投票中计算出他/她的得分:

(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)))]
    )
  )

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM