Scheme (DrRacket) - Calling Generalized / Abstracted Function with Another Function -
for reference programming scheme using drracket.
for problem making generalized / abstracted function (which aren't using higher-order functions and/or lambda) called tally-by
of function tally-by-place-points
defined below:
(define listofcandidates (list "blake" "ash" "bob" "will" "joey")) ;; signature: tally-by-place-points: ;; list-of-candidates list-of-votes -> list-of-voting-tallies ;; purpose: consumes list of candidate names , list of votes , ;; produces list of voting-tallies. ;; (points-per-place strategy). ;; tests: (check-expect (tally-by-place-points empty empty) empty) (check-expect (tally-by-place-points listofcandidates listofvotes) (cons (make-voting-tally "blake" 7) (cons (make-voting-tally "ash" 3) (cons (make-voting-tally "bob" 5) (cons (make-voting-tally "will" 1) (cons (make-voting-tally "joey" 2) empty)))))) ;; define: (define (tally-by-place-points aloc alov) (cond [(empty? aloc) empty] [else (cons (make-voting-tally (first aloc) (total-points-for (first aloc) alov)) (tally-by-place-points (rest aloc) alov))]))
this came (not sure if correct):
;; signature: tally-by: (helper function) ;; list-of-candidates list-of-votes -> list-of-voting-tallies ;; purpose: consumes helper function, list of candidate names, ;; , list of votes , produces list of voting-tallies. ;; define: (define (tally-by helper aloc alov) (cond [(empty? aloc) empty] [else (cons (make-voting-tally (first aloc) (tally-by helper (first aloc) alov)) (tally-by helper (rest aloc) alov))]))
i should note helper function referring to, total-points-for
:
;; signature: total-points-for: string list-of-strings -> number ;; purpose: consumes name , list of votes , produces ;; number of points given name has received ;; using points-per-place strategy. ;; tests: (check-expect (total-points-for "ash" empty) 0) (check-expect (total-points-for "ash" listofvotes) 3) (check-expect (total-points-for "blake" listofvotes) 7) (check-expect (total-points-for "bob" listofvotes) 5) (check-expect (total-points-for "will" listofvotes) 1) (check-expect (total-points-for "joey" listofvotes) 2) (check-expect (total-points-for "brad" listofvotes) 0) ;; define: (define (total-points-for cand alov) (cond [(empty? alov) 0] [(string=? (vote-choice1 (first alov)) cand) (+ 3 (total-points-for cand (rest alov)))] [(string=? (vote-choice2 (first alov)) cand) (+ 2 (total-points-for cand (rest alov)))] [(string=? (vote-choice3 (first alov)) cand) (+ 1 (total-points-for cand (rest alov)))] [else (total-points-for cand (rest alov))]))
i have modify tally-by-place-points
function call generalized / abstracted function tally-by
made. should note signature, purpose, , check-expects correct. function came with, although definition not correct:
;; signature: tally-by-place-points: ;; list-of-candidates list-of-votes -> list-of-voting-tallies ;; purpose: consumes list of candidate names , list of votes ;; , produces list of voting-tallies. ;; (points-per-place strategy). ;; tests: (check-expect (tally-by-place-points empty empty) empty) (check-expect (tally-by-place-points listofcandidates listofvotes) (cons (make-voting-tally "blake" 7) (cons (make-voting-tally "ash" 3) (cons (make-voting-tally "bob" 5) (cons (make-voting-tally "will" 1) (cons (make-voting-tally "joey" 2) empty)))))) ;; define: (define (tally-by-place-points aloc alov) (cond [(empty? aloc) empty] [else (cons (make-voting-tally (first aloc) (tally-by (first aloc) alov)) (tally-by (rest aloc) alov))]))
i hoping able assist me tally-by
, modified tally-by-place-points
function definitions unsure do.
in tally-by-place-points call (total-points-for (first aloc) alov)
place points. in tally-by
supposed able same if pass total-points-for
argument helper
. need replace references total-points-for
helper
, pass helper when recursing:
(define (tally-by helper aloc alov) (cond [(empty? aloc) empty] [else (cons (make-voting-tally (first aloc) (helper (first aloc) alov)) (tally-by helper (rest aloc) alov))]))
in tally-by-place-points
using tally-by
call tally
argument got correct helper
:
(define (tally-by-place-points aloc alov) (tally-by total-points-for aloc alov))
btw mention of language using ambiguous. syntax programming racket in drracket. guess first line either #lang scheme
(#!scheme
short) or #!racket
, synonyms , not official scheme language. actual scheme language need use #!r6rs
or #!r5rs
, program according those.
Comments
Post a Comment