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

  • 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)

  • Need help in c++ programming to output the lines in my code: if word if found:...

    Need help in c++ programming to output the lines in my code: if word if found:            cout << "'" << word << "' was found in the grid" << endl;            cout << "'" << word << "' was not found in the grid" << endl; The words can be touching if they are horizontally, vertically, or diagonally adjacent. For example, the board: Q W E R T A S D F G Z X C V B Y U A...

  • 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...

  • Previous Problem Problem List Next Problem f(x, y) (1 point) Consider the function f(x, y) =...

    Previous Problem Problem List Next Problem f(x, y) (1 point) Consider the function f(x, y) = (e* - 5x) sin(y). Suppose S is the surface z (a) Find a vector which is perpendicular to the level curve of f through the point (5,4) in the direction in which f decreases most rapidly. vector -(eA5-5)sin(4)i+-(e^5-5(5)cos(4)j (b) Suppose above (5,4). What is a? 2i 8jak is a vector in 3-space which is tangent to the surface S at the point P lying...

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
ADVERTISEMENT