Code the function (insertNth list N insValue) which constructs a new list by inserting the specified insValue into the list after the Nth top-level value (relative to 1).Example:> (insertNth '(X Y Z) 2 'FUN)(X Y FUN Z)> (insertNth '(X Y Z) 4 'FUN)(X Y Z)

Respuesta :

Answer:

Explanation:

Function algorithm is coded below

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

2. Replace in Function

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(defun replace-all (string part replacement &key (test #'char=))

 "Returns a new string in which all the occurrences of the part is replaced with replacement."

 (with-output-to-string (out)

   (loop with part-length = (length part)

         for old-pos = 0 then (+ pos part-length)

         for pos = (search part string

                           :start2 old-pos

                           :test test)

         do (write-string string out

                          :start old-pos

                          :end (or pos (length string)))

         when pos do (write-string replacement out)

         while pos)))

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

3. Insert After

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(defun insert-after (lst index newelt)

(push newelt (cdr (nthcdr index lst)))

lst)

(insert-after '(a c d) 0 'b) => (A B C D)

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

4. insertNth

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(defun insertNth(number index list)

 (do ((head '() (list* (first tail) head))

      (tail list (rest tail))

      (index index (1- index)))

     ((zerop index)

      (nreconc head (list* (+ number (first tail))

                           (rest tail))))))

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

5. InsertAfterAll

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

(defun InsertAfterAll (a v)

  (if (null v) (cons a nil) (cons (car v) (endcons a (cdr v)))))

(endcons 'a '(b c d))

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////