Question

In Lisp 2. Code the function (replaceIn list possibleList repValue) which constructs a new list. It...

In Lisp

2. Code the function (replaceIn list possibleList repValue) which constructs a new list. It examines list for occurrences of any of the atoms from the possibleList. Those are replaced with repValue. This only examines the top-level items in list.

Example:

> (replaceIn '(P A T T E R) '(T R) 'S)

(P A S S E S)

3.   Code the function (insertAfter list atm insValue) which constructs a new list by inserting the specified insValue into the list after each top-level occurrence of the specified atm.

Example:

> (insertAfter '(H O H O) 'H 'X)

(H X O H X O)

> (insertAfter '(H O H O) 'W 'X)

(H O H O)

4.   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)

5.   Code the function (insertAfterAll list atm insValue) which constructs a new list by inserting the specified insValue into the list after all occurrences of the specified atm. This includes any level of nesting.

Example:

> (insertAfterAll '(X (X Y X) X Z) 'X 'W)

(X W (X W Y X W) X W Z)

> (insertAfterAll '((X (X (Y X)) X) Z) 'X 'W)

((X W (X W (Y X W)) X W) Z)

6.   Code the function, (atomicList list), which is passed a list that can have embedded lists. It should return a list of atoms that occur anywhere in the list regardless of nesting.

   Hint: APPEND can be useful.

Examples:

> (atomiclist '(A (B F (H) G) J))

(A B F H G J)

> (atomiclist '(L () (I () S) (((P ()))) ))

(L NIL I NIL S P NIL)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

please find the answer below

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

2. Replcae in Funciton

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

(defun replace-all (string part replacement &key (test #'char=))
  "Returns a new string in which all the occurences 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))

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

Add a comment
Know the answer?
Add Answer to:
In Lisp 2. Code the function (replaceIn list possibleList repValue) which constructs a new list. It...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2. Write a LISP function COUNTLETS which takes a list and returns the number of top-level...

    2. Write a LISP function COUNTLETS which takes a list and returns the number of top-level alphabetic atoms in the list. For example: COUNTLETS ( (A 2) B 3 C4 (D 5))) Returns the value 2, the letters A and D are not at the top level

  • this is part of my code im not sure how to do last 2 function. i...

    this is part of my code im not sure how to do last 2 function. i think above last 2 functions need for making function please help me how to do it. #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include "list.h" typedef struct node { ElemType val; struct node *next; } NODE; struct list_struct { NODE *front; NODE *back; }; int lst_remove_first(LIST *l, ElemType x) { NODE *p; NODE *tmp; if(l->front == NULL) return 0; if(l->front->val == x) {    lst_pop_front(l);...

  • Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42,...

    Please explain the python code below L1 = [2, 15, 'Carol', 7.4, 0, -10, -6, 42, 27, -1, 2.0, 'hello', [2, 4], 23] print("L1 =",L1) odds =[] evens=[] list=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',',','.'] no=0 for i in L1: i=str(i) for j in list: if i.find(j)>=0: no=1 if no==1: None else: i=int(i) if i%2==0: evens.append(i) else: odds.append(i) no=0      ...

  • these questions are about LISP please explain the reason In the following Fun language example, which...

    these questions are about LISP please explain the reason In the following Fun language example, which expressions can we get by some series of reductions? Assume that we can reduce the built-in function + in the usual way that addition works. Definitions: f(x, y) = x+y g(x) = x+1 h(x) = 7 Example: f(g(h(4)), h(g(5))) 1.Select all that apply: a. g(h(4)) + h(g(5)) b. f(g(7), h(g(5))) c. f(8, 7) d. 15 e. f(g(h(4)) + h(g(5))) f. f(5, h(g(5))) g. f(h(g(4)),...

  • Submit the pseudo code. Write a member function PrintReverse that prints the elements on a list...

    Submit the pseudo code. Write a member function PrintReverse that prints the elements on a list in reverse order. For instance, for the list X Y Z, list. PrintReverse() would output element in the list. You may assume that the list is not empty.

  • In problem 3, you will write a function, findvertically (), which will determine whether or not...

    In problem 3, you will write a function, findvertically (), which will determine whether or not a given word exists in a word search puzzle vertically. In word search puzzles, words can be written upwards or downwards. For example, "BEAK" appears at [1] [3] written downwards, while "BET" appears at [2] [2] written upwards: [["C", "A", "T", "X", "R"], ["D", "T", "E", "B", "L"], ["A" "R" "B", "E", "Z"], ["X", "O", "E", "A", "U"], ["J", "S", "O", "K", "W"]] Your...

  • Using Python, Implement a function that returns an opposite of a dictionary: In order to decrypt...

    Using Python, Implement a function that returns an opposite of a dictionary: In order to decrypt the encrypted text, we will use the opposite of the substitution dictionary. Opposite dictionary of a dictionary is the dictionary where each value is the key and the key is the value. For instance, for the dictionary above, its opposite dictionary is {'I': 'A', 'T': 'B', 'R': 'C', 'A': 'D', 'N': 'E', 'S': 'F', 'L': 'G', 'O': 'H', 'M': 'I', 'W': 'J', 'U': 'K',...

  • Q3. A system's behavior is governed by the following transfer function relationships. Draw a block diagram...

    Q3. A system's behavior is governed by the following transfer function relationships. Draw a block diagram to represent this scenario. X(s) = G($)U(s); Y(s) = H($)U(s); W(S) = M(9)Y(s); Z(s) = P(s)X(s); T(s) = Q(s)Y(s), N(s) = Z(s) + W(s) +T(s)

  • I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import...

    I need help with my python class. thanks! Question 12 Code Example 4-4 main program: import arithmetic as a def multiply(num1, num2):     product = num1 * num2     result = a.add(product, product)     return result     def main():     num1 = 4     num2 = 3     answer = multiply(num1, num2)     print("The answer is", answer) if __name__ == "__main__":     main() arithmetic module: def add(x, y):     z = x + y     return z Refer to Code...

  • on.ca i.cmd med to u nunaown C eSA COwPA 20 or 2. Write a function that...

    on.ca i.cmd med to u nunaown C eSA COwPA 20 or 2. Write a function that takes a single list of characters as argument and returns a new list of integers as a return value. The list returned must contain the ASCII codes of every uppercase character in the original list in the reverse order from the order they appeared in the original list. You are not permitted to use negative indexing (i.e., the numbers you write in the square...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT