简体   繁体   中英

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.

#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) . And (apply + ...) is adding all the elements in the sublist.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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