[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Help counting duplicate items in list with Scheme

Posted on 2011-04-24
Medium Priority
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

fridom earned 2000 total points
ID: 35482277
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

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Screencast - Getting to Know the Pipeline

873 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