<p>I have been searching everywhere for the following functionality in Lisp, and have gotten nowhere:</p>

<ol>
<li><p>find the index of something in a list. example:</p>

<pre><code>(index-of item InThisList)
</code></pre></li>
<li><p>replace something at a specific spot in a list. example:</p>

<pre><code>(replace item InThisList AtThisIndex) ;i think this can be done with 'setf'?
</code></pre></li>
<li><p>return an item at a specific index. example:</p>

<pre><code>(return InThisList ItemAtThisIndex)
</code></pre></li>
</ol>

<p>Up until this point, I've been faking it with my own functions. I'm wondering if I'm just creating more work for myself.</p>

<p>This is how I've been faking number 1:</p>

<pre><code>(defun my-index (findMe mylist)
  (let ((counter 0) (found 1))
    (dolist (item mylist)
      (cond
        ((eq item findMe) ;this works because 'eq' checks place in memory, 
                  ;and as long as 'findMe' was from the original list, this will work.
         (setq found nil)
        (found (incf counter))))
  counter))
</code></pre>

[{'answer_id': 45233, 'author': 'Jeremy', 'author_id': 1114, 'author_profile': 'https://Stackoverflow.com/users/1114', 'pm_score': 6, 'selected': True, 'text': '<p>You can use <code>setf</code> and <code>nth</code> to replace and retrieve values by index.</p>\n\n<pre><code>(let ((myList \'(1 2 3 4 5 6)))\n     (setf (nth 4 myList) 101); &lt;----\n     myList)\n\n(1 2 3 4 101 6)\n</code></pre>\n\n<p>To find by index you can use <a href="http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/html/cltl/clm/node145.html" rel="nofollow noreferrer">the <code>position</code> function</a>.</p>\n\n<pre><code>(let ((myList \'(1 2 3 4 5 6)))\n     (setf (nth 4 myList) 101)\n     (list myList (position 101 myList)))\n\n((1 2 3 4 101 6) 4)\n</code></pre>\n\n<p>I found these all <a href="http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/html/cltl/clm/index.html" rel="nofollow noreferrer">in this index of functions</a>.</p>\n'}, {'answer_id': 45670, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p>Jeremy\'s answers should work; but that said, if you find yourself writing code like</p>\n\n<p>(setf (nth i my-list) new-elt)</p>\n\n<p>you\'re probably using the wrong datastructure. Lists are simply linked lists, so they\'re O(N) to access by index. You might be better off using arrays.</p>\n\n<p>Or maybe you\'re using lists as tuples. In that case, they should be fine. But you probably want to name accessors so someone reading your code doesn\'t have to remember what "nth 4" is supposed to mean. Something like</p>\n\n<pre><code>(defun my-attr (list)\n  (nth 4 list))\n\n(defun (setf my-attr) (new list)\n  (setf (nth 4 list) new))\n</code></pre>\n'}, {'answer_id': 53872, 'author': 'ujh', 'author_id': 4936, 'author_profile': 'https://Stackoverflow.com/users/4936', 'pm_score': 0, 'selected': False, 'text': '<p>I have to agree with Thomas. If you use lists like arrays then that\'s just going to be slow (and possibly awkward). So you should either use arrays or stick with the functions you\'ve written but move them "up" in a way so that you can easily replace the slow lists with arrays later.</p>\n'}, {'answer_id': 53937, 'author': 'Sébastien RoccaSerra', 'author_id': 2797, 'author_profile': 'https://Stackoverflow.com/users/2797', 'pm_score': 4, 'selected': False, 'text': '<blockquote>\n  <ol>\n  <li>find the index of something in a list.</li>\n  </ol>\n</blockquote>\n\n<p>In Emacs Lisp and Common Lisp, you have the <code>position</code> function:</p>\n\n<pre><code>&gt; (setq numbers (list 1 2 3 4))\n(1 2 3 4)\n&gt; (position 3 numbers)\n2\n</code></pre>\n\n<p>In Scheme, here\'s a tail recursive implementation from <a href="http://www.drscheme.org/" rel="noreferrer">DrScheme</a>\'s doc:</p>\n\n<pre><code>(define list-position \n  (lambda (o l)\n    (let loop ((i 0) (l l))\n      (if (null? l) #f\n          (if (eqv? (car l) o) i\n              (loop (+ i 1) (cdr l)))))))\n\n----------------------------------------------------\n\n&gt; (define numbers (list 1 2 3 4))\n&gt; (list-position 3 numbers)\n2\n&gt; \n</code></pre>\n\n<p>But if you\'re using a list as a collection of slots to store structured data, maybe you should have a look at <code>defstruct</code> or even some kind of Lisp Object System like CLOS.</p>\n\n<p>If you\'re learning Lisp, make sure you have a look at <a href="http://gigamonkeys.com/book/" rel="noreferrer">Practical Common Lisp</a> and / or <a href="http://www.ccs.neu.edu/home/matthias/BTLS/" rel="noreferrer">The Little Schemer</a>.</p>\n\n<p>Cheers!</p>\n'}, {'answer_id': 63572, 'author': 'Eric Normand', 'author_id': 7492, 'author_profile': 'https://Stackoverflow.com/users/7492', 'pm_score': 3, 'selected': False, 'text': '<p>Answers:</p>\n\n<ol>\n<li><p>(position item sequence &amp;key from-end (start 0) end key test test-not)<br>\n<a href="http://lispdoc.com/?q=position&amp;search=Basic+search" rel="noreferrer">http://lispdoc.com/?q=position&amp;search=Basic+search</a>  </p></li>\n<li><p>(setf (elt sequence index) value)</p></li>\n<li><p>(elt sequence index)<br>\n<a href="http://lispdoc.com/?q=elt&amp;search=Basic+search" rel="noreferrer">http://lispdoc.com/?q=elt&amp;search=Basic+search</a><br>\nNOTE: elt is preferable to nth because elt works on any sequence, not just lists</p></li>\n</ol>\n'}, {'answer_id': 66599, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 2, 'selected': False, 'text': '<p><em>+2</em> for "Practical Common Lisp".  It is a mixture of a Common Lisp Cookbook and a quality Teach Yourself Lisp book.</p>\n\n<p>There\'s also "Successful Common Lisp" (<a href="http://www.psg.com/~dlamkins/sl/cover.html" rel="nofollow noreferrer">http://www.psg.com/~dlamkins/sl/cover.html</a> and <a href="http://www.psg.com/~dlamkins/sl/contents.html" rel="nofollow noreferrer">http://www.psg.com/~dlamkins/sl/contents.html</a>) which seemed to fill a few gaps / extend things in "Practical Common Lisp".</p>\n\n<p>I\'ve also read Paul Graham\'s "ANSI Common Lisp" which is more about the basics of the language, but a bit more of a reference manual.</p>\n'}]
['https://Stackoverflow.com/questions/45227', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/50/']