Question

In: Computer Science

You can only use built in Lisp functions and you cannot use setq function. Write a...

You can only use built in Lisp functions and you cannot use setq function.

Write a function in Lisp called f1 that counts the number of lists in a list. Example: (f1 ‘(a (a b (b c)) c d (e))) returns 2

Solutions

Expert Solution

check out the solution.

--------------------------

CODE:

; f1 function implementation
(defun f1 (lst)
; multiple condition list
(cond ((null lst) 0) ; if list is empty then return 0
; check each list item is list if yes then count it and recursive call
((listp (car lst)) (+ 1 (f1 (cdr lst))))
; else recursive call without counting
(t (f1 (cdr lst)))
)
)

; function call and print the result
(print (f1 '(a (a b (b c)) c d (e))))
(print (f1 '(a (a b (b c)) c d)))
(print (f1 '(a (a b) (b (c)) (c) d)))

---------------------------------------

-----------------------------------------------------------------------

OUTPUT :

=============================================


Related Solutions

which statements are true about Python functions? a)Different functions cannot use same function name b)a function...
which statements are true about Python functions? a)Different functions cannot use same function name b)a function always returns some value c)different function cannot use the same variable names d) function must use the same parameter names as the corresponding variables in the caller what benefits does structuring a program through defining functions bring? a) there is a possibility of reducing the number of variables and/or objects that must be managed at any cost at any one point b)the program is...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (merge-sorter L1) that takes list-of-integers L1 and returns all elements of L1 in sorted order. You must use a merge-sort technique that, in the recursive case, a) splits L1 into two approximately-equal-length lists, b) sorts those lists, and then c) merges the...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (forget-n L1 N) that returns the elements of L1 except for the first N. If N is negative, return all elements. If N exceeds the length of L1 return the empty list....
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem needs to use DrRacket software. Racket Language. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarification....
You must write each of the following scheme functions. You must use only basic scheme functions,...
You must write each of the following scheme functions. You must use only basic scheme functions, do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function named (first-n L1 N) that returns the first N elements of L1. If N is negative, return the empty list. If N exceeds the length of L1 return all elements of L1. (first-n...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. This problem need to use DrRacket software. Racket Language. Write a function (join-together L1 L2) that takes a sorted list (ascending) of integers L1 and a sorted list of elements L2 and returns a sorted list containing all elements of both L1 and L2. See...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function (indices L1 X) that takes a list of elements L1 and an element X. The function returns a list of the indices in L1 that contain X. See the following examples for clarificaton. (indices '(a b c a e f a) 'a')...
You must write each of the following scheme functions. You must use only basic scheme functions...
You must write each of the following scheme functions. You must use only basic scheme functions do not use third-party libraries to support any of your work. Do not use any function with side effects. Write a function named (list-replace ALIST SYM VAL) that accepts a list of elements and returns that list where all SYM's (a single symbol) have been replaced by the VAL (some scheme value). The replacement must occur even within nested lists. For example: (list-replace '(a...
Directly using mathematical expression, DO NOT USE the built-in Python functions, evaluate the binomial function for...
Directly using mathematical expression, DO NOT USE the built-in Python functions, evaluate the binomial function for n = 20, p = 3/8. Make sure you plot your results (Remember the the binomial distribution is discrete)
LISP FUNCTIONS Imagine you are writing the AI for a simple game that will require an...
LISP FUNCTIONS Imagine you are writing the AI for a simple game that will require an adversarial search. The state of the game can be represented using a list of 3 elements, such as (B B B). The list representing the game state can contain any combination of 3 values: A, B, or C. It will always have exactly 3 items. (Let's call it the "state list"). With that in mind, write the following LISP functions: TERMINAL-TEST - this function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT