<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
Marek Kubica wrote at 03/16/2009 08:32 AM:<br>
<blockquote cite="mid:20090316133253.78688ff4@halmanfloyd.lan.local"
 type="cite">
  <blockquote type="cite">
    <pre wrap="">To do it functionally, remember that building a result is easy in a 
recursive algorithm if you're cons-ing a list, and also that in
Scheme you can *read* characters at arbitrary positions in a string.

Also, I don't think you need to build an explicit list of positional 
numbers.  Think about how those numbers change at each step of the 
algorithm, and how you can do that in recursive function calls in one 
pass, at the same time you are building the result.
    </pre>
  </blockquote>
  <pre wrap=""><!---->
I wasn't able to figure out how to get the numbers in one pass. I'll
look into Davids code to find out how he did it.
  </pre>
</blockquote>
<br>
Methodically writing down a table mapping destination index to source
index might help give an intuition for the numbers.<br>
<br>
If you figure out a function for the numbers, then something like the
following code should work.&nbsp; In this example, the function is just
taking every second letter from the source string and wrapping around
when it runs out.<br>
<br>
(define (foo source-string)<br>
<br>
&nbsp; (let ((source-length (string-length source-string)))<br>
&nbsp;&nbsp;&nbsp; <br>
&nbsp;&nbsp;&nbsp; (define (bar destination-remaining x-value)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (zero? destination-remaining)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; '()<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (let ((new-x-value (modulo (+ x-value 2) source-length)))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; Above we have our function that maps positions in<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; the destination to positions in the source.&nbsp; It<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; could be a function of "destination-remaining",<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; "x-value", and/or other values.&nbsp; In this case, at<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; each step of assembling the destination list,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; "x-value" at this step is a function of "x-value"<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; at the preceding step.&nbsp; The "x-value" for this<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; step is then passed to the next recursive step.<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (cons (string-ref source-string new-x-value)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (bar (- destination-remaining 1)<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; new-x-value)))))<br>
<br>
&nbsp;&nbsp;&nbsp; (list-&gt;string (bar source-length -2))))<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br>
(foo "abcdefghijklmnopqrstuvwxyz")<br>
;=&gt; "acegikmoqsuwyacegikmoqsuwy"<br>
<br>
</body>
</html>