?
Solved

Scheme  recursion question (part 2)

Posted on 2011-04-21
9
Medium Priority
?
580 Views
Last Modified: 2012-05-11
I need to write a function that accepts a single string (a b a a a c c)
that counts the number of repeated occurrences, yielding ((1 a) (1 b) (3 a) (2 c)).
0
Comment
Question by:InfoTechEE
  • 4
  • 3
  • 2
9 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35445062
This one you really do need a helper function.
Helper functions work like this:
You call the main function which calls the helper function which calls itself. So the main function isn't recursive in itself. You need this because you need to pass an integer for the count. Then you just need to compare car and car(cdr) and pass cdr to the helper function for the recursion.
So you'll have count_dups lst which will call count_dupsH count lst
Does that make sense?
0
 

Author Comment

by:InfoTechEE
ID: 35445090
Yes, but I'm not sure what each function is suppose to do.

Here's what I'm understanding thus far:

(count_dups lst)
no recursion happens here...just calls the helper function
not sure what to pass to the helper function here

(count_dups count lst)
recursion occurs in this function, but under what circumstances?
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35445123
(count_dups lst)
no recursion happens here...just calls the helper function

Right
not sure what to pass to the helper function here
Well, the helper function needs a count so pass it whatever the count should start as (0 or 1 depending on how you do the rest of it)

(count_dups count lst)
recursion occurs in this function, but under what circumstances?

I would do it just like the other two where you consume one character every time and keep making recursive calls 'til the string runs out.

So you have a count and a string and you compare the first two characters. Assuming count always equals the current count of the duplicates,
If they match, what should you do?
If they don't match, then what?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:InfoTechEE
ID: 35445400
I'm thinking if 1st and 2nd element match, I need to increment the counter.
If they dont match, just output a 1 and car(lst)?
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 35445421
Yes. Except of course don't just output a 1, output whatever the counter currently is (which should be 1 the first time of course).
0
 

Expert Comment

by:TechLearnerCA
ID: 35457310
InfoTechEE

can you show me how you solved this problem?

thanks!
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35457383
TechLearner, all the information is there to solve it. What else could you possibly need? Are you just looking to have someone give you the full code because you have the same assignment?

What you should do is hit 'ask a related question' and post your own question asking for clarification. Don't just ask for an answer though. Since this is an academic style problem, we are instructed to help you solve it yourself. So include the code you have so far and what it is and isn't doing that it shouldn't or should be.
0
 

Expert Comment

by:TechLearnerCA
ID: 35457445
Hi TommySzalapski, I am not looking for someone do my work for me.

I posted a question: http://www.experts-exchange.com/Programming/Misc/Q_26974518.html

and I have not gotten any responses. Could you look at my question, please?
0
 

Expert Comment

by:TechLearnerCA
ID: 35457496
I tried to implement what was discussed above and I have some errors:
procedure application: expected procedure, given: 1 (no arguments)
(define (countrepeat lst)
  
  (count((1) (lst)))

    )

(define (count s lst)
  (cond
    
    ((null? list) '())
    ((null? (cdr lst)) '())
    
   
    
    ((equal? (car lst)(car(cdr lst))) (count(+1 cdr lst)))
    
    (else (list s(list(car lst))))
    ))

Open in new window

0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

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

If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
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 …
Progress

592 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