Thanks. Atleast I can get a nested list now.... Will ask again if I am still stuck. <div><br><br><div class="gmail_quote">On Sat, May 8, 2010 at 9:42 AM, Stephen Bloch <span dir="ltr"><<a href="mailto:sbloch@adelphi.edu">sbloch@adelphi.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><div style="word-wrap:break-word">
<br><div><div class="im"><div>On May 7, 2010, at 11:55 PM, Aniket Karmarkar wrote:</div><br><blockquote type="cite"><div>That's what this function does:</div><div>(define (equal L1 L2)</div><div> (cond</div><div> ((eq? (caar L1) (car L2)) (car L1))</div>
<div> (else (equal (cdr L1) L2))</div><div> )</div><div>)</div><div><br> </div><div>This function find the sublist which match. I had it before but I forgot to include it. So it returns (b c d). I tried the map function but you need the arguments to have same number in both arguments. :(</div>
</blockquote><div><br></div></div>There's a standard trick to get around that.</div><div><br></div><div>Suppose I want to make a list of the two-element lists ' (a x), '(b x), '(c x), '(d x). I'm doing the same thing to each element of the list '(a b c d), and producing a list of the results, so "map" seems like the right tool. What is it that I want to do to each of these elements? Create a pair with that element and x. So I write a function</div>
<div>(define (pair-with-x something)</div><div> (list something x))</div><div>then call</div><div>(map pair-with-x '(a b c d))</div><div><br></div><div>Sound good so far? It has a problem: it only works if you have a FIXED x that you can hard-code into the "pair-with-x" function. If the thing you want to combine with is a parameter, or has just been computed, or something like that, you define "pair-with-x" LOCALLY:</div>
<div><br></div><div>(define (pair-with-a-b-c-d x)</div><div> (define (pair-with-x something) (list something x))</div><div> (map pair-with-x '(a b c d)))</div><div><br></div><div>Make sense?</div><div><br></div><div>
Now, a brief digression. Suppose you wanted to compute 3+4*5. One way to do it would be in two steps:</div><div>(define temp (* 4 5))</div><div>(+ 3 temp)</div><div>But if the only reason for having "temp" is to use it in one subsequent expression, why bother giving it a name? It's shorter and simpler to say</div>
<div>(+ 3 (* 4 5))</div><div><br></div><div>Similarly, Scheme allows you to define a function without bothering to give it a name, if you're just going to use it once, using "lambda".</div><div><br></div><div>
(define (pair-with-a-b-c-d x)</div><div> (map (lambda (something) (list something x)) '(a b c d)))</div><div><br></div><div>In general, anything you can do with a local definition can also be done with lambda, possibly shorter, and anything you can do with lambda can also be done with a local definition, possibly clearer (if you like having names for things).</div>
<div><br></div><div>Hope that helps!</div><div class="im"><br><br><div> <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;text-align:auto;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><div>
Stephen Bloch</div><div><a href="mailto:sbloch@adelphi.edu" target="_blank">sbloch@adelphi.edu</a></div><div><br></div></span><br> </div><br></div></div></blockquote></div><br></div>