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)
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)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT