We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

Scheme  recursion question (part 2)

Medium Priority
690 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)).
Comment
Watch Question

CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
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?

Author

Commented:
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?
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
(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?

Author

Commented:
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)?
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
InfoTechEE

can you show me how you solved this problem?

thanks!
CERTIFIED EXPERT
Awarded 2010
Top Expert 2013

Commented:
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.
Hi TommySzalapski, I am not looking for someone do my work for me.

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

and I have not gotten any responses. Could you look at my question, please?
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

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.