<div dir="ltr">Thanks a lot Dan and Matthias. Your explanations are very helpful.<div><br></div><div>Best,</div><div>Rian</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Jan 13, 2014 at 7:06 PM, Daniel Prager <span dir="ltr"><<a href="mailto:daniel.a.prager@gmail.com" target="_blank">daniel.a.prager@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi Rian<div><br></div><div><span style="font-family:arial,sans-serif;font-size:13px">> are there are any built-in functions that I can take advantage of to make this function cleaner?</span><br>
</div><div>
<br></div><div>I'm not aware of a built-in function to help, but sometimes it's quite easy to abstract upwards and write your own.</div><div><br></div><div>In this case a generalization to map for trees that preserves structure looks about right:</div>
<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>;; Apply f to every atom in 'tree', preserving the list-y structure.</div></div><div><div>;;</div></div><div><div>(define (tree-map f tree)</div>
</div><div><div> (define (T t)</div></div><div><div> (cond [(empty? t) empty]</div></div><div><div> [(atom? (first t)) (cons (f (first t)) (T (rest t)))]</div></div><div><div> [else (cons (T (first t)) (T (rest t)))]))</div>
</div><div><div> (T tree))</div></div><div><br></div></blockquote><div>> (tree-map (ë (x) '*) '(+ (- .1 .2) .3)) </div><div>'(* (* * *) *)</div><div><div><br></div><div>Then instead of mapping to an asterisk, you can use an incrementing counter:</div>
<div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>(define (make-counter from)</div></div><div><div> (let ([index from])</div></div><div><div> (ë ()</div></div><div><div> (let ([result index])</div>
</div><div><div> (set! index (add1 index))</div></div><div><div> result))))</div></div><div><br></div></blockquote><div>> (define c (make-counter 1))</div><div>> (c)</div><div>1</div><div>> (c)</div>
<div>2</div></div><div><br></div><div>Now you can write your indexing function succinctly:</div><div><br></div><blockquote style="margin:0 0 0 40px;border:none;padding:0px"><div><div>(define (index-s-exp s-exp index)</div>
</div><div><div> (let ([c (make-counter index)])</div></div><div><div> (tree-map (ë (x) (c)) s-exp)))</div></div></blockquote><div><br></div><div>Aside: This is a less pure solution compared to Matthias's, since it makes use of state in the counter.</div>
<div><br></div><div><br></div><div>Dan</div></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br>Rian Shams
</div>