<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<br><div><div>On May 7, 2010, at 10:42 PM, Aniket Karmarkar wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">This is what I am trying to do.<div><span class="Apple-style-span" style="font-family: monospace; font-size: medium; white-space: pre; ">Write the function partialpaths, of two arguments. The first argument is a list of lists, representing a graph(adjacency list representation of a graph). The second argument is a partial path in the reverse order, for example the path "from a to b to c" is represented by the list (c b a). The function should return a list of sublists containing all possible expansions of this partial path by one node. For example: (partialpaths '( (a b c) (b c d) (c d a) (d)) '(b a)) should return: ( (c b a) (d b a)) </span><br></div><div>I get the (b c d) part now I need to attach the cdr of this to b a multiple times so I try putting it in a loop.&nbsp;</div><div><br></div><div>Here's what I have:</div><div><div>(define nod ())</div> <div>(define ma '('()))</div><div>(define (partialpaths L1 L2)</div><div>&nbsp;&nbsp;(set! nod (equal L1 L2))</div><div>&nbsp;&nbsp;(do()</div><div>&nbsp;&nbsp; &nbsp;;exit test</div><div>&nbsp;&nbsp; &nbsp;((null? (cdr nod)))</div><div>&nbsp;&nbsp; &nbsp;(list ma (attach(cdr nod)L2))</div> <div>&nbsp;&nbsp; &nbsp;(set! nod (cdr nod))</div><div>&nbsp;&nbsp;)</div><div>&nbsp;&nbsp;ma</div><div>)</div></div></blockquote><div><br></div>Yigg. &nbsp;Are&nbsp;the loop, the mutation, and the global variables really necessary? &nbsp;With all due respect, this doesn't look like Scheme code to me :-)</div><div><br></div><div><blockquote type="cite"><div><div>(define (attach List1 List2)</div><div>&nbsp;&nbsp;(list (car List1) List2)</div><div>)</div></div></blockquote><div><br></div>This almost certainly doesn't do what you think it does.</div><div><br>Seems to me what you want to do is extract the first (i.e. last) element of the path, find the row of the table that starts with it, and cons each of the other elements of that row onto the path.</div><div><br></div><div>So off the top of my head, I would define a variable equal to (car path), then another equal to the row (if any) in the adjacency matrix whose car matches it (this may require a helper function), then map over the cdr of the row a function which conses its argument onto the path. &nbsp;I did this in about six lines (not counting test cases) with no loops, no mutation, no global variables, and only one recursion (in the helper function).</div><div><br></div><br><br><div> <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: Helvetica; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: auto; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0; "><div>Stephen Bloch</div><div><a href="mailto:sbloch@adelphi.edu">sbloch@adelphi.edu</a></div><div><br class="webkit-block-placeholder"></div></span><br class="Apple-interchange-newline"> </div><br></body></html>