Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

veryconfused

I've got (define (lod-clean lst) (cond [(empty? lst) empty] [(and (> (first lst) 0)(empty? (rest lst))) (first lst)] [(and (= (first lst) 0)(empty? (rest lst))) empty] [else (cons (first lst) (lod-clean (rest lst)))])) but when i test (lod-clean (list 1 2 3 4 0 0)) i get (list 1 2 3 4 0), i need to get rid of all trailing zeros, please help @Computer Science

  • 2 years ago
  • 2 years ago

  • This Question is Closed
  1. Narsat
    Best Response
    You've already chosen the best response.
    Medals 0

    Try to use terminal recursivity since you're trying to work from the end to the start. It should be something like... (define (lod-clean list) (lod-clean-aux list '())) (define (lod-clean-aux list r) (cond ((empty? list) r) ((> (car list) 0) (lod-clean-aux (cdr list) (append r (list (car list)))))))) First method works only to call the aux method that actually does the job. It should work though I can't test it at the moment. car = first cdr = rest r = aux list to which values are passed to and will be returned in the end.

    • 2 years ago
  2. dmancine
    Best Response
    You've already chosen the best response.
    Medals 0

    @veryconfused, Try putting several 0's at the end of your test list. It's only removing the very last 0 (not all but one). Also, if your test list ends with a non-zero number you're not returning a list. Your base cases seem to be where you've put the important logic. You're modifying the return value, so once you reach the base case you'll start returning from the recursive calls, and all you'll do is cons the head of the list to the return value. I created another function, zeros?, which returns #t if the list is empty or full of zeros. As I cdr down the list in lod-clean I check zeros? each time. When it's #t I start returning, and cons the car of the current list to the return value. I don't like it because in the worst case (a list of all zeros with a non-zero at the very end) it's O(n^2). And it's not even tail recursive. (It's been a long time since I've had to think in Scheme.)

    • 2 years ago
  3. dmancine
    Best Response
    You've already chosen the best response.
    Medals 0

    @Narsat, I couldn't get your code to return anything with any test case.

    • 2 years ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.