简体   繁体   English

如何将球拍方案列表加在一起

[英]How to add together a racket scheme list

New to Racket Scheme and attempting to add together the contents of a defined list. 球拍方案的新手,尝试将已定义列表的内容加在一起。 I am to add together the list individually such as the totals of each individual list and then the totals of all of the list together. 我要分别将列表加在一起,例如每个单独列表的总数,然后是所有列表的总数。

So far what i have is the defined list and what i believe to be an inital addition of the totals of the list but it appears that nothing is being sum'd together. 到目前为止,我所拥有的是已定义的列表,我认为是列表总数的初始加法,但似乎没有任何东西加在一起。 How can i add together all of the values in this list and all the list individually as well? 如何将此列表中的所有值以及所有列表分别加在一起?

CODE

#lang racket
(define team '(("Emp1" (57 57 80 47 68 56 84 65))
               ("Emp2" (57 69 57 84 87 71 77 69 61 48))
               ("Emp3" (46 47 61 65 81 64 40 77 51 78))
               ("Emp4" (70 68 89 41))
               ("Emp5" (45 48 74 83 40 44 70 85 98 86))
               ))

(define (getEmpTotals team)
  (if (empty? team) 0
      (+ (first team)(getEmpTotals(rest team)))))

Function to Implement 实现功能

(getTeamTotal team)

Desired Output 期望的输出

(("Emp1" 514) ("Emp2" 680) ("Emp3" 610) ("Emp4" 268) ("Emp5" 673))

2745

Your (first team) is extracting a the list ("Emp1" (57 57 80 47 68 56 84 65)) , which you can't use + on. 您的(first team)正在提取列表("Emp1" (57 57 80 47 68 56 84 65)) ,您不能在其上使用+

#lang racket

(require rackunit)

;; A Team is a [Listof [list String [Listof Number]]]
;; or
;; A Team is a [Listof Emp]
;; An Emp is a [list String [Listof Number]]

(define team '(("Emp1" (57 57 80 47 68 56 84 65))
               ("Emp2" (57 69 57 84 87 71 77 69 61 48))
               ("Emp3" (46 47 61 65 81 64 40 77 51 78))
               ("Emp4" (70 68 89 41))
               ("Emp5" (45 48 74 83 40 44 70 85 98 86))))

;; [Listof Number] -> Number
;; sums every number in l
(define (lsum l)
  (apply + l))

;; a recursive version, if you want:
(define (lsum-rec l)
  (if (empty? l)
      0
      (+ (first l) (lsum-rec (rest l)))))


;; you can map lsum on the [Listof Number] part of the team
(define (emp-sum team)
  (map (λ (empl) (list (first empl) (lsum (second empl)))) team))

(check-equal? (emp-sum team)
              '(("Emp1" 514) ("Emp2" 680) ("Emp3" 610) ("Emp4" 268) ("Emp5" 673)))

(define (team-sum team)
  (lsum (map second (emp-sum team))))

(check-equal? (team-sum team)
              2745)

The problem lies in this part: 问题出在这部分:

(first team)

The first team is a list of two elements, an identifier and a sublist, and we want to add all the elements in that sublist. 第一个团队是一个包含两个元素的列表 ,一个标识符和一个子列表,我们希望将所有元素添加到该子列表中。 Here's how: 这是如何做:

(define (getEmpTotals team)
  (if (empty? team)
      0
      (+ (apply + (first (rest (first team))))
         (getEmpTotals (rest team)))))

The (first (rest (first team))) part can be simplified as (cadar team) . (first (rest (first team)))部分可以简化为(cadar team) And (apply + ...) is adding all the elements in the sublist. 并且(apply + ...)正在添加子列表中的所有元素。

As a different alternative, we can use higher-order procedures to write a more idiomatic solution: 作为另一种选择,我们可以使用更高阶的过程来编写更惯用的解决方案:

(define (getEmpTotals team)
  (foldl (λ (t acc) (+ (apply + (cadr t)) acc))
         0
         team))

Either way, it works as expected: 无论哪种方式,它都能按预期工作:

(getEmpTotals team)
=> 2745

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

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