Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You want to use <a href="http://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket/private/list..rkt%29._filter%29%29" rel="nofollow"><code>filter</code></a>, not <a href="http://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Fmap..rkt%29._map%29%29" rel="nofollow"><code>map</code></a> - because the output list will potentially have less elements than the input list. All those <code>#&lt;void&gt;</code> values returned by <code>display</code> are there because <code>map</code> will always include a result in the output list, <em>even</em> for those elements we're not interested in.</p> <pre><code>(define tbl '(a b c d)) (filter (lambda (item) (eq? item 'c)) tbl) =&gt; '(c) </code></pre> <p>Equivalently, and a bit shorter:</p> <pre><code>(filter (curry eq? 'c) tbl) =&gt; '(c) </code></pre> <p><code>map</code> is used when you want to do something to each of the elements in the input list, without discarding elements. On the other hand, <code>filter</code> is used for selecting <em>some</em> of the elements in the input list, those that evaluate to <code>#t</code> for a given predicate, and <code>filter</code> is available in most Scheme interpreters, if it's not available you can import <a href="http://srfi.schemers.org/srfi-1/srfi-1.html#FilteringPartitioning" rel="nofollow"><code>SRFI-1</code></a> or use the <a href="http://srfi.schemers.org/srfi-1/srfi-1-reference.scm" rel="nofollow">reference implementation</a>.</p> <p>There's no way to obtain <code>'(c)</code> using <em>only</em> <code>map</code> (it can be hacked using <code>map</code> plus <code>apply</code> or <code>remove*</code>, etc. but that's not the idea, is it?); if for some reason you <em>have to</em> use only <code>map</code> and don't mind returning a list with placeholders, here are a couple of alternatives:</p> <pre><code>(map (lambda (item) (if (eq? item 'c) item '%)) tbl) ; placeholder in else part =&gt; '(% % c %) (map (lambda (item) (when (eq? item 'c) item)) tbl) ; when has implicit #&lt;void&gt; =&gt; '(#&lt;void&gt; #&lt;void&gt; c #&lt;void&gt;) </code></pre> <p>Time for a little hacking. Using <code>map</code> plus <a href="http://docs.racket-lang.org/reference/procedures.html#%28def._%28%28lib._racket%2Fprivate%2Fbase..rkt%29._apply%29%29" rel="nofollow"><code>apply</code></a> (as explained in @WillNess' answer), this has the advantage of working in any RxRS interpreter and is the most portable solution, because it uses standard procedures:</p> <pre><code>(apply append (map (lambda (item) (if (eq? item 'c) (list item) '())) tbl)) =&gt; '(c) </code></pre> <p>Using <code>map</code> plus <a href="http://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._remove%2A%29%29" rel="nofollow"><code>remove*</code></a>:</p> <pre><code>(remove* (list (void)) (map (lambda (item) (when (eq? item 'c) item)) tbl)) =&gt; '(c) </code></pre> <p>For a change, a solution without <code>map</code> - using <a href="http://docs.racket-lang.org/reference/pairs.html#%28def._%28%28lib._racket%2Fprivate%2Flist..rkt%29._foldr%29%29" rel="nofollow"><code>foldr</code></a> instead:</p> <pre><code>(foldr (lambda (item a) (append (if (eq? item 'c) (list item) '()) a)) '() tbl) =&gt; '(c) </code></pre> <p>Of course, you can always implement your own version of <code>filter</code> using only standard procedures, this will also be portable across all RxRS interpreters:</p> <pre><code>(define (filter pred? lst) (cond ((null? lst) '()) ((not (pred? (car lst))) (filter pred? (cdr lst))) (else (cons (car lst) (filter pred? (cdr lst)))))) (filter (lambda (item) (eq? item 'c)) tbl) =&gt; '(c) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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