Help counting duplicate items in list with Scheme

Posted on 2011-04-24
Last Modified: 2012-05-11
Hi I am given a list( a b a a a a c c c) and I need to return ((1 a)(1 b)(3 a)(2c))

I am stuck and not sure how to make this work.
Here is the code I have so far:
(define (countrepeat lst)
    ((null? lst) '())
    ((null? (cdr lst)) (list(car lst)))
    ((equal? (car lst)(car(cdr lst))) (cons(list(car lst(count(car lst))))(countrepeat(cdr lst))))
    (else ((list(1 car lst))(countrepeat(cdr lst))))

(define (count lst)
    ((null? list) 0)
    ((=(car lst)(car(cdr lst)))(+1 (count(cdr lst))))

Open in new window

Question by:TechLearnerCA
    LVL 24

    Accepted Solution

    Well your code does  not what it is supposed to do. Especially the count function will not work as expected.

    You want another function which just runs through the list once and counts the elements as it walks the list.
    I keep track of a few elements during that wal (the read element) and a counter all togehter we have:

    (define (counter-list element counter lst result-list)
      (let ((first-element (unless (null? lst)(car lst))))
          ((null? lst) 
            (reverse (cons (list counter element) result-list)))
          ((equal? first-element element)
               (counter-list element (1+ counter) (cdr lst) result-list))
           (counter-list first-element 1 (cdr lst) (cons (list counter element) result-list))))))
    (define (test lst)
      (counter-list (car lst) 0 lst '()))
    (test '( a b a a a a c c c))
    Willkommen bei DrScheme, Version 4.1 [3m].
    Sprache: Swindle angepasst; memory limit: 256 megabytes.
    ((1 a) (1 b) (4 a) (3 c))

    Open in new window

    LVL 53

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    character entities and entity references in XML 17 84
    bigHeights  challenge 13 43
    bunnyEars challenge 6 46
    strDist challenge 35 70
    This article will show, step by step, how to integrate R code into a R Sweave document
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    23 Experts available now in Live!

    Get 1:1 Help Now