Question

In: Computer Science

Write a function in lisp XIT that counts the number of items in each sub-list of...

Write a function in lisp XIT that counts the number of items in each sub-list of a list and returns the count in a list. It should only work if there are two levels of brackets in the parameter (simple lists inside the parent list). Thus:

            (XIT '((A B C)))       

-> (3)

            (XIT '((A) (A B) (A B C)))

-> (1 2 3)

            (XIT '((1 2)))             

-> (2)

            (XIT '(1 (2 3)))           

-> '(ILLEGAL)

            (XIT '((1 2 (3))))        

-> '(ILLEGAL)

Solutions

Expert Solution

Answer: The below code does the required task.

Here a helper function '(check (lst))' is used. This function takes in a list as a parameter and returns 'T' if all the elements of if are not lists. Else it returns 'nil'.

Code:

check function:-

(defun check (lst)
       (setq len2 (length lst))
       (setq j 0)
       (loop
           (setq tmp2 (nth j lst))
           (if (listp tmp2)
               (return nil)
           )
           (setq j (+ j 1))
           (when (= j len2)
               (return t)
           )
       )
   )

XIT function:-

(defun XIT (a)
       (if (not (listp a))
           (progn (return ''(illegal)))
           (progn
               (setq len (length a))
               (setq i 0)
               (setq ret '())
               (loop
                   (setq tmp (nth i a))
                   (if (not (listp tmp))
                       (progn (return ''(illegal)))
                       (if (not (check tmp))
                           (progn (return ''(illegal)))
                           (progn
                               (setq l (length tmp))
                               (setq ret (append ret (list (eval l))))
                               (setq i (+ i 1))
                               (when (= i len) (return ret))
                           )
                       )
                   )
               )
           )
       )
   )

Screenshots:


Related Solutions

Write a LISP function COUNTX which takes an atom and a list and returns the number...
Write a LISP function COUNTX which takes an atom and a list and returns the number of top-level occurrences of the atom in the list. For example: (COUNTX ‘A ‘(A (A B) B A B A (B A)) Returns the value 3, the other two A’s are not at the top level
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length...
*LISP PROGRAM* 2. Write a function that, given a list of lists, returns the total length of all the lists. This problem can be solved two different ways. 3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers. 4.Write ALLODDP, a recursive function that returns T if all the numbers in a list are odd.
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
Write a LISP function POWER that takes a list containing exactly two numeric atoms and computes...
Write a LISP function POWER that takes a list containing exactly two numeric atoms and computes the first atom raised to the power of the second atom. For example: (POWER ‘(2 4) Returns the value 16
Write a function that counts the number of times a given integer occurs in a Linked...
Write a function that counts the number of times a given integer occurs in a Linked List. What is the time complexity of your algorithm? Justify your answer in python
CODE USING LISP... No other language please trim-to (symbol list) Write a function named trim-to that...
CODE USING LISP... No other language please trim-to (symbol list) Write a function named trim-to that takes a symbol and list as parameters. Return a new list starting from the first occurrence of the input symbol in the input list. If there is no occurrence of the symbol, return nil. For example: (trim-to ‘c ‘(a b c d e)) This should return the following list: ‘(c d e) ackermann (number number) Write a function named ackermann that takes two integers....
C programming review excerises. 1. Write a function that counts the number of lines in a...
C programming review excerises. 1. Write a function that counts the number of lines in a file, using the following declaration: int countLines(char *filename) 2. Write a program that counts the number of words in a text file. Use the command-line arguments to read the name of the file. The syntax: countwords filename.txt 3. Write a program that counts the number of words starting with the first letter ‘T’ in a text file. Using commend line argument for the text...
I need to write a function that counts the number of total wins and losses. I...
I need to write a function that counts the number of total wins and losses. I have a text file called analysis_data.txt with 1000 lines written Won Loss Won Loss Won Won ... The function need to do the following: Opens the analysis_data.txt file and reads through all the data, counting number of 'Won' and 'Loss' words stored in the file. Returns two integers: count of wins and count of losses from the text file. **Can't use the break statement
(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n...
(1) Write a simple Lisp function factorial1 to computee factorial number of n recursivaly (where n is an int >= 0). (2) Write a Lisp function factorial2 to computee factorial number of n (where n is an int >=0) recursivaly with a global variable (e.g., a list, an array, or a hash tble) to save the result to be used later (memorization) to compute next factorial number. Run the program with test case. For your test run, the following cases:...
Write a C program that counts the number of odd numbers with using function count() within...
Write a C program that counts the number of odd numbers with using function count() within the set. The set has only one negative number which determines the end of set.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT