Question

In: Computer Science

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

Solutions

Expert Solution

Answer

(defun COUNTX (a lst) ;function COUNTX that takes an atom and a list

(cond

((null lst) 0) ;return 0 when list is empty

((equal a (car lst)) ;if given atom is the head of the list

(+ 1 (COUNTX a (cdr lst)))) ;add 1 to the value returned by the recursive call of COUNTX

(t (COUNTX a (cdr lst))))) ;else, call COUNTX with the rest of the given list

Output

Note - Please let me know if you have any query.


Related Solutions

Write LISP functions GEFILTER and LFILTER. GEFILTER takes a numeric atom and a list of numbers...
Write LISP functions GEFILTER and LFILTER. GEFILTER takes a numeric atom and a list of numbers and returns a list consisting all numbers in the list which are greater than or equal to the given number. LFILTER takes a numeric atom and a list of numbers and returns a list consisting of all numbers in the list less than the given number. For example: (GEFILTER 4 ‘(3 5 8 2 4 1 9)) Returns the list (5 8 4 9)...
*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.
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.
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)))...
Write a function that takes a number as input, and returns the character A if the...
Write a function that takes a number as input, and returns the character A if the input is 90 and above, B if it’s 80 and above but less than 90, C if it’s at least 70 but less than 80, D if it’s at least 60 but less than 70, and F if it’s less than 60. If the input is not a number or is negative, the function should exit 1 with an error (by calling the Matlab...
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 Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
We were asked this: Write a function that takes a list, and returns a list representing...
We were asked this: Write a function that takes a list, and returns a list representing each word whose reverse is also in the list. def find_reversals(lst: List[str]) -> List[str]: Each pair, such as 'abut', 'tuba', should be represented by the first element encountered. Don't report the same pairs twice. Don't list palindromes. I wrote this, which might not be optimal but passes the unit test! def find_reversals(lst): #Place to save to found_reversals=[]    #Make each string lowercase for i...
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]...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT