Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Try this:</p> <pre><code>(define (replacecoresp ls a-list) (cond ((null? ls) '()) ((not (list? ls)) (cond ((assq ls a-list) =&gt; cadr) (else ls))) (else (cons (replacecoresp (car ls) a-list) (replacecoresp (cdr ls) a-list))))) </code></pre> <p>It works as expected:</p> <pre><code>(replacecoresp '(I have (a list) of (list (list and list))) '((list queue) (have not))) &gt; (I not (a queue) of (queue (queue and queue))) </code></pre> <p>Explanation: When traversing a list of lists (say, <code>ls</code>) you need to consider three cases:</p> <ol> <li><code>ls</code> is empty, return the empty list</li> <li><code>ls</code> is an atom not a list, process the element</li> <li><code>ls</code> is a list, invoke the recursion on both the <code>car</code> and the <code>cdr</code> of the list and combine the results</li> </ol> <p>In the particular case of your question, <code>cons</code> is used in the third step for combining the solution; and the second case is the part where we check to see if the current symbol is in the association list, replacing it if it was found or leaving the symbol untouched if not. I used a shortcut for writing less code in this step, but you can replace the inner <code>cond</code> with this snippet of code if it's clearer:</p> <pre><code>(let ((lookup (assq ls a-list))) (if lookup (cadr lookup) ls)) </code></pre> <p>Another way to express the solution is to use a <code>map</code> on the list like this:</p> <pre><code>(define(replacecoresp ls a-list) (if (not (list? ls)) (cond ((assq ls a-list) =&gt; cadr) (else ls)) (map (lambda (l) (replacecoresp l a-list)) ls))) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload