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

*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.
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....
(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:...
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
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a function which receives a list and returns a number. In the list, all numbers...
Write a function which receives a list and returns a number. In the list, all numbers have been repeated twice except one number that is repeated once. The function should return the number that is repeated once and return it.write a python program for this question. use main function.
This is a Combinatorics question. Find a generating function for a sub r, the number of...
This is a Combinatorics question. Find a generating function for a sub r, the number of ways: (1) To distribute ridentical objects into seven distinct boxes with an odd numbet of objects not exceeding nine in the first three boxes and between four and ten in the other boxes.
In a program, write a function that accepts two arguments: a list, and a number n....
In a program, write a function that accepts two arguments: a list, and a number n. Assume that the list contains numbers. The function should display all of the numbers in the list that are greater than the number n. The program should ask for a list of numbers from the user as well as a value (a, b, c)--> inputs from user. After that, each number in that list should be compared to that value (a or b or...
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT