Home > Blockchain >  "pair required" error when trying to count how many times the first number is repeated in
"pair required" error when trying to count how many times the first number is repeated in

Time:06-18

I am trying to count how many times the first number of the list of list called one, two or three is repeated, the idea of the code that I made is for example to choose the list of list called "one" and then take the first letter that has "a" inside and then compare in this case the first value that would be "10" with the others in this list , in this case I should get it to say that the "10" was repeated once, then it would continue with the other two "b" and "c", but when doing the following code

(define a (list 10 2 3 54 6 9 7 10))
(define b (list 5 1 8 6 5 5 4 77 8 6))
(define c (list 80 80 80))
(define e (list 99 156 54 48 99))
(define d (list 16 94 75 30 56 16 8 16))
(define one (list a b c))
(define two (list c e b a))
(define three (list b c d e))

; receives 'one', 'two' or 'three' and finds how many times the first number of the list is repeated in the list of lists
(define (find-repeated-number list)
  (define (find-repeated-number-helper list)
    (if (null? list)
        0
      (if (equal? (car list) (car (cdr list)))
          (  1 (find-repeated-number-helper (cdr list)))
          ; displays the number of times the first number of the list is repeated in the list of lists
        (find-repeated-number-helper (cdr list)))))
  (find-repeated-number-helper list))

(find-repeated-number one)

I get the following error, why is it? how can i get what i want to do? I would appreciate any help

*** ERROR: pair required, but got ()
     While loading "./jdoodle.sc" at line 19
Stack Trace:
_______________________________________
   0 (car (cdr list))
         at "./jdoodle.sc":14
   1 (equal? (car list) (car (cdr list)))
         at "./jdoodle.sc":14
Command exited with non-zero status 70

CodePudding user response:

I'm still not sure if the main point of this exercise is to write low-level recursive code or use some functions provided by the standard library (in this case, count or a combination of filter and length), but here's my wild guess:

(define a (list 10 2 3 54 6 9 7 10))
(define b (list 5 1 8 6 5 5 4 77 8 6))
(define c (list 80 80 80))
(define e (list 99 156 54 48 99))
(define d (list 16 94 75 30 56 16 8 16))
(define one (list a b c))
(define two (list c e b a))
(define three (list b c d e))
    
(define (find-count lst element)
    (count (lambda (n) (= n element)) 
                        lst))
                    
(define (print-count lst)
    (let ((number-count (find-count (cdr lst) (car lst))))
        (write (string-append 
                            "number of times the first number is repeated: "
                            (number->string (car lst))
                            " is "
                            (number->string number-count)
                            (if (= number-count 1) 
                                " time"
                                " times")))
        (newline)))
    
(for-each print-count one)

Output:

"number of times the first number is repeated: 10 is 1 time"
"number of times the first number is repeated: 5 is 2 times"
"number of times the first number is repeated: 80 is 2 times"
  • Related