<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 11:55 PM, Aniket Karmarkar wrote:</div><br class="Apple-interchange-newline"><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>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><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>