<p>I have a function that gets x(a value) and xs(a list) and removes all values that are bigger than x from the list. Well it doesn't work, can you tell me why?</p>

<pre><code>(defun biggerElems(x xs) 
  (let ((xst))
    (dolist (elem xs)
      (if (&gt; x elem)
          (setf xst (remove elem xs))))
    xst))
</code></pre>

[{'answer_id': 46437, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 4, 'selected': True, 'text': "<p>I think it's this line that's not right:</p>\n\n<pre><code>(setf xst (remove elem xs))))\n</code></pre>\n\n<p>The first argument to <code>setf</code> is the place, followed by the value.  It looks like you have it backwards (and <code>xst</code> is either <code>nil</code> or uninitialized).</p>\n\n<p>You might find it easier to do this:</p>\n\n<pre><code>(defun biggerElems (x xs)\n  (remove-if (lambda (item) (&gt; item x)) xs))\n</code></pre>\n"}, {'answer_id': 46474, 'author': 'Alexander Stolz', 'author_id': 2450, 'author_profile': 'https://Stackoverflow.com/users/2450', 'pm_score': 1, 'selected': False, 'text': "<p>It worked like that:</p>\n\n<pre><code>(defun filterBig (x xs)\n  (remove-if (lambda (item) (&gt; item x)) xs))\n</code></pre>\n\n<p>What was the '#' for? It didn't compile with it.</p>\n"}, {'answer_id': 46494, 'author': 'Ben Collins', 'author_id': 3279, 'author_profile': 'https://Stackoverflow.com/users/3279', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <p>What was the '#' for? It didn't\n  compile with it.</p>\n</blockquote>\n\n<p>Typo.  Normally you refer to functions with <code>#'</code> (like <code>(remove-if #'oddp list)</code>), but when I was editing, I forgot to remove the '#'.</p>\n"}, {'answer_id': 46500, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 1, 'selected': False, 'text': "<p>If you want to do this the Lisp Way, you could use recursion to return the new list:</p>\n\n<pre><code>(defun biggerElems (x xs)\n  (cond ((null xs) NIL)\n        ((&lt; x (car xs))\n         (biggerElems x (cdr xs)))\n       (t\n        (cons (car xs) (biggerElems x (cdr xs))))))\n</code></pre>\n\n<p>@Luís Oliveira</p>\n\n<p>This solution is to contrast with the one posted in the question. Had we needed to do something slightly more complicated it's important to be grounded in the recursive approach to manipulating lists. </p>\n"}, {'answer_id': 72912, 'author': 'Bart', 'author_id': 9792, 'author_profile': 'https://Stackoverflow.com/users/9792', 'pm_score': 2, 'selected': False, 'text': "<p>Most concise AFAIK:</p>\n\n<pre><code>(defun bigger-elements (x xs) (remove x xs :test #'&lt;))\n</code></pre>\n\n<p>returning a fresh list, it removes all elements y from xs for which </p>\n\n<pre><code>(&lt; y x)\n</code></pre>\n\n<p>or using the famous LOOP:</p>\n\n<pre><code>(defun bigger-elements-2 (x xs) \n  (loop for e in xs\n        unless (&lt; e x)\n        collect e))\n</code></pre>\n"}, {'answer_id': 118049, 'author': 'Morikal', 'author_id': 18272, 'author_profile': 'https://Stackoverflow.com/users/18272', 'pm_score': 1, 'selected': False, 'text': "<p>@Ben: It isn't the setf call that is wrong--the problem is that he is not updating xs.</p>\n\n<p>ie: xst is being set to xs with the element removed, but xs is not being updated. If a second element is to be removed, xst will have the first back in it.</p>\n\n<p>you would need to bind xst to xs, and replace the xs in the remove call with xst. This would then remove all elements x is bigger than. ie:</p>\n\n<pre><code>(defun biggerElems(x xs)\n  (let ((xst xs))\n    (dolist (elem xs)\n      (when (&gt; x elem)\n        (setf xst (remove elem xst))))\n    xst))\n</code></pre>\n\n<p>It might be slightly faster to set xst to (copy-list xs) and then use delete instead of remove (delete is destructive... depending on your implementation, it may be faster than remove. Since you are calling this multiple times, you may get better performance copying the list once and destructively deleting from it).</p>\n\n<p>Alternatively:</p>\n\n<pre><code>(defun bigger-elems (x xs) ; I prefer hyphen separated to camelCase... to each his own\n  (loop for elem in xs when (&lt;= x elem) collect elem))\n</code></pre>\n\n<p>Looking back over your original post, it is a little confusing... you say you remove all elements bigger than x, but your code looks like it is attempting to remove all elements x is bigger than. The solutions I wrote return all elements bigger than x (ie: remove all elements x is bigger than).</p>\n"}]
['https://Stackoverflow.com/questions/46425', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2450/']