Help with scheme code.

Discussion of Scheme and Racket

Help with scheme code.

Postby helpneededhere » Thu Oct 06, 2011 4:37 am

I really need help with this coding here. I would be grateful to anyone who helps me out with this.


Use accumulative recursion to write a function, expand, that consumes a nonempty string and produces a string that has been reversed where there is a space between each character in the original string. For example (expand “Hi guys.”) produces “. s y u g i H").

Do I have to convert the string into the list and then back again to make the code work or should I try to reverse the string as a string?
helpneededhere
 
Posts: 1
Joined: Thu Oct 06, 2011 4:34 am

Re: Help with scheme code.

Postby saulgoode » Fri Oct 07, 2011 12:24 am

helpneededhere wrote:Do I have to convert the string into the list and then back again to make the code work or should I try to reverse the string as a string?

While not necessary, it would certainly simplify things.

Accumulative recursion just means that an extra parameter is passed to your function each time it is called; this extra parameter holds the accumulated result.

For example,
Code: Select all
(define (evens-in-list lis result)
  (if (null? lis)
    result
    (evens-in-list (cdr lis)
                   (if (even? (car lis))
                     (cons (car lis) result)
                     result ))))

You would call this function with the list of numbers you are testing (lis) along with your "seed" value for the result (which initially should be the empty list). Each time through, an element of the list is examined and if it is even then it is prepended onto the head of the resulting list.

Now it is quite common that the user doesn't care to be bothered with having to specify the initial accumulated value, it is just needed in order for the recursion to keep track of the result. For this reason, you will often see the accumulated function defined within another function; one that doesn't have the accumulated result parameter.

Code: Select all
(define (evens-in-list lis)
  (define (evens-in-list-iter lis result)
    (if (null? lis)
      result
      (evens-in-list-iter (cdr lis)
                          (if (even? (car lis))
                            (cons (car lis) result)
                            result ))))
  (evens-in-list-iter lis '()) )

Note that the -iter function could be defined completely separately (outside of 'evens-in-list'), but by placing it within 'evens-in-list', we keep things more organized. Also, the order of the even numbers in the result will be opposite how they appear in the original list, so you might want to 'reverse' them or perform other processing (by modifying the last line).

Now your function is going to be passed a list of characters, and needs to prepend both the character and a space to the result each time through the iteration. This is accomplished with something such as "(cons #\space (cons (car lis) result))". You may also have to test each original character to avoid duplicating spaces (per your example).
saulgoode
 
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am


Return to Scheme

Who is online

Users browsing this forum: No registered users and 0 guests

cron