0

I have a task to solve and unfortunately came to a point where I am not sure If I am on the right path at all. The setting is a phone, where you have a list of calls each with the structure (make-call Date Phone Duration) and I have to summarize all calls from a certain number (Phone) and create a list of contacts with each number, that summarizes the duration (make-contact Phone Duration).

So the question is how do I e.g. get from

(list (make-call Date1 "123" 1.5) (make-call Date2 "123" 4) (make-call Date3 "456" 2))

to

(list (make-contact "123" 5.5) (make-contact "456" 2))

My plan is to first filter a list of calls for a certain name:

;(define CALL1 (make-call "18.01." "101010" 4.5))
;(define CALL2 (make-call "01.01." "222222" 1.0))
;(define CALL3 (make-call "13.01." "123456" 20.2))
;(define CALL4 (make-call "05.01." "999999" 10))

; (list-of-Call) Name  -> (List-of-Call)
; filters all calls with a certain name from a list of calls
(check-expect (filter-by-number empty "101010") empty)
(check-expect (filter-by-number (list CALL1 CALL1) "101010") (list CALL1 CALL1))
(check-expect (filter-by-number (list CALL1 CALL2) "Jonas") empty)

(define (filter-by-number lst nam)
  (if (empty? lst)
      empty
      (if (string=? (call-callee (first lst)) nam)
          (append (filter-by-number (first lst) nam) (filter-by-number (rest lst) nam))
          empty)))

Unfortunately this function doesn't work properly yet.

In a second step, my plan is to put this togehter somehow to create the required list of contacts, and that where I am stuck:

;(List-of-Call) -> (List-of-Contact)
;computes the total duration of all calls of a certain number
(check-expect (compute-contacts empty) empty)
(check-expect (compute-contacts (list CALL1)) (list (make-contact "101010" 4.5)))
(check-expect (compute-contacts (list CALL1 CALL2 CALL3)) (list (make-contact "101010" 4.5) (make-contact "222222" 1.0) (make-contact "123456" 20.2)))
(check-expect (compute-contacts (list CALL4 CALL4)) (list (make-contact "999999" 20)))
(check-expect (compute-contacts (list CALL4 CALL4 CALL1) (list (make-contact "999999" 20))) (make-contact "101010" 4.5))

(define (summarize-contacts lst)
  (if (empty? lst)
      empty
      (append (make-contact ...) (make-contact ...))))

How would you do proceed from here? Or would you do it a totally different way? Any hints welcome :)

Cheers!

Illmatic
  • 1
  • 1
  • It feels strange that you're calling `filter-by-number` on the `(first lst)`, which is a single `Call` structure, while the signature of `filter-by-number` says it takes a `(List-of Call)` for the first argument. If you're using the Design Recipe, there should be a natural recursion on the `rest`, but not on the `first` – Alex Knauth Jan 19 '20 at 17:24
  • Someone was asking almost the same question: https://stackoverflow.com/questions/59815728/racket-bsl-how-can-i-combine-two-instances-of-a-structure-in-a-list-that-have-o/59816269?noredirect=1#comment105785650_59816269 – vonschlager Jan 21 '20 at 09:25

0 Answers0