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)...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of...
1.Write a function div7(lst) which takes in a list of integers, and returns a list of booleans of the same length, such that for each integer in the original list, the boolean in the output list is True if that integer was divisible by 7, or False if not. Use list comprehensions in python, the function only could be at most two lines long. Here is some examples: >>> div7([14, 5, 7, 3, 29, 28, 10]) [True, False, True, False,...
Write a function that takes a list of integers as input and returns a list with...
Write a function that takes a list of integers as input and returns a list with only the even numbers in descending order (Largest to smallest) Example: Input list: [1,6,3,8,2,5] List returned: [8, 6, 2] Do not use any special or built in functions like append, reverse etc.
*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...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a...
In DrRacket Write a function, removeAll, which takes two lists, list-a and list-b and returns a list containing only the items in list-a that are not also in list-b. E.g., (remove-all '(a b b c c d) '(a c a)) -> '(b b d)
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT