Function Scheme(DrRacket)-用另一个函数调用广义/抽象函数
作为参考,我正在使用DrRacket使用Scheme编程 对于这个问题,我制作了一个广义/抽象函数(不使用高阶函数和/或lambda),称为函数Function Scheme(DrRacket)-用另一个函数调用广义/抽象函数,function,scheme,racket,abstraction,Function,Scheme,Racket,Abstraction,作为参考,我正在使用DrRacket使用Scheme编程 对于这个问题,我制作了一个广义/抽象函数(不使用高阶函数和/或lambda),称为函数按位置点计数的计数,定义如下: (define listofCandidates (list "Blake" "Ash" "Bob" "Will" "Joey")) ;; Signature: tally-by-place-points: ;; list-of-candidates list-of-votes ->
按位置点计数的计数,定义如下:
(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 a list of candidate names and a list of votes and
;; produces a 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))]))
这就是我想到的(不确定是否正确):
;; Signature: tally-by: (helper function)
;; list-of-candidates list-of-votes -> list-of-Voting-Tallies
;; Purpose: Consumes a helper function, a list of candidate names,
;; and a list of votes and produces a 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))]))
;; Signature: tally-by-place-points:
;; list-of-candidates 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.
;; (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))]))
我应该注意,这是我所指的帮助函数,总分为
:
;; Signature: total-points-for: string list-of-strings -> number
;; Purpose: Consumes a name and a list of votes and produces the
;; number of points that the given name has received
;; using a 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))]))
我现在必须修改tallybyplacepoints
函数,以调用我刚刚创建的广义/抽象函数tallybys
。我应该注意签名、目的和支票都是正确的。这是我提出的函数,尽管定义不正确:
;; Signature: tally-by: (helper function)
;; list-of-candidates list-of-votes -> list-of-Voting-Tallies
;; Purpose: Consumes a helper function, a list of candidate names,
;; and a list of votes and produces a 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))]))
;; Signature: tally-by-place-points:
;; list-of-candidates 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.
;; (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))]))
我希望有人能够帮助我完成我的tall by
和修改的tall by place points
函数定义,因为我不确定该怎么做。在tall by place points中,您调用(第一次aloc)alov的总积分)
来获取位置点。在tally by
中,如果您将总分作为参数helper
传递,您应该能够做到完全相同的操作。因此,您需要将对
的总分的引用替换为helper
,并在递归时传递helper:
(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))]))
在tally by place points
中,使用tally by
只需调用tally
by,并使用正确的辅助对象得到的参数即可:
(define (tally-by-place-points aloc alov)
(tally-by total-points-for aloc alov))
顺便说一句,你提到你使用的是哪种语言是模棱两可的。从语法上看,您很可能是在DrRacket中编程racket。我猜你的第一行是#lang scheme
(#!scheme
)或者#!racket
,是同义词,不是正式的Scheme语言。对于实际的Scheme语言,您需要使用#!r6rs
或#!R5R
,并根据其进行编程 按您调用的地点点进行理货(第一个aloc)alov的总分)
以获取地点点。在tally by
中,如果您将总分作为参数helper
传递,您应该能够做到完全相同的操作。因此,您需要将对
的总分的引用替换为helper
,并在递归时传递helper:
(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))]))
在tally by place points
中,使用tally by
只需调用tally
by,并使用正确的辅助对象得到的参数即可:
(define (tally-by-place-points aloc alov)
(tally-by total-points-for aloc alov))
顺便说一句,你提到你使用的是哪种语言是模棱两可的。从语法上看,您很可能是在DrRacket中编程racket。我猜你的第一行是#lang scheme
(#!scheme
)或者#!racket
,是同义词,不是正式的Scheme语言。对于实际的Scheme语言,您需要使用#!r6rs
或#!R5R
,并根据其进行编程 按您调用的地点点进行理货(第一个aloc)alov的总分)
以获取地点点。在tally by
中,如果您将总分作为参数helper
传递,您应该能够做到完全相同的操作。因此,您需要将对
的总分的引用替换为helper
,并在递归时传递helper:
(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))]))
在tally by place points
中,使用tally by
只需调用tally
by,并使用正确的辅助对象得到的参数即可:
(define (tally-by-place-points aloc alov)
(tally-by total-points-for aloc alov))
顺便说一句,你提到你使用的是哪种语言是模棱两可的。从语法上看,您很可能是在DrRacket中编程racket。我猜你的第一行是#lang scheme
(#!scheme
)或者#!racket
,是同义词,不是正式的Scheme语言。对于实际的Scheme语言,您需要使用#!r6rs
或#!R5R
,并根据其进行编程 按您调用的地点点进行理货(第一个aloc)alov的总分)
以获取地点点。在tally by
中,如果您将总分作为参数helper
传递,您应该能够做到完全相同的操作。因此,您需要将对
的总分的引用替换为helper
,并在递归时传递helper:
(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))]))
在tally by place points
中,使用tally by
只需调用tally
by,并使用正确的辅助对象得到的参数即可:
(define (tally-by-place-points aloc alov)
(tally-by total-points-for aloc alov))
顺便说一句,你提到你使用的是哪种语言是模棱两可的。从语法上看,您很可能是在DrRacket中编程racket。我猜你的第一行是#lang scheme
(#!scheme
)或者#!racket
,是同义词,不是正式的Scheme语言。对于实际的Scheme语言,您需要使用#!r6rs
或#!R5R
,并根据其进行编程 抽象是一种纯粹的语法转换!相反,这只是测试版的减少:
..... a ..... ==> (λa. ..... a ..... ) a
也就是说,您只需将使用的某个实体放入函数的参数中,并在函数调用中将该实体作为参数传递。这样,以前特定的东西现在被概括,变成一个参数,抽象为:
(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))]))
被改写为
(define (tally-by place-points aloc alov)
(cond ; ~~~~~~~~~~~~
[(empty? aloc) empty]
[else (cons (make-voting-tally (first aloc)
(place-points (first aloc) alov))
; ^^^^^^^^^^^^^^^^
(tally-by-place-points (rest aloc) alov))]))
所以电话
(tally-by total-points-for aloc alov)
相当于上一次呼叫
(tally-by-place-points aloc alov)
这当然需要在递归调用中进行整理-它也需要进行转换:
(define (tally-by place-points aloc alov)
(cond ; ~~~~~~~~~~~~
[(empty? aloc) empty]
[else (cons (make-voting-tally (first aloc)
(place-points (first aloc) alov))
(tally-by place-points (rest aloc) alov))]))
; ~~~~~~~~~~~~
因为我们将(按地点点理货…
)替换为(按地点点理货…
——无处不在
当然,我们现在可以用我们想要的任何名称(只要名称是唯一的)重命名placepoints
参数。抽象是一种纯粹的语法转换!它只是beta-reduce