Question

In: Computer Science

*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.

Solutions

Expert Solution

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.

(defun totalLength(mylist)
(setq total 0)
(loop
    (setq total (+ total (length (car mylist))))
    (setf mylist (cdr mylist))
    (if (endp mylist) (return))
   )
   total
)

(format t "~%Total length of list: ~s ~%~%" (totalLength '((a) (a b) (a b c))))

output

3. Write a program that prompts the user to enter two numbers and then outputs the sum of the two numbers.

(write-line "Enter first number")
(setq number1 (read))
(write-line "Enter second number")
(setq number2 (read))
(setq total (+ number1 number2))
(format t "~%~s+~s=~s ~%" number1 number2 total)

output

Completed 2 questions. For the answer of last question post a new post with only that question.


Related Solutions

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
Given a list of items, write a program that generates a list of lists of the...
Given a list of items, write a program that generates a list of lists of the following form: [a,b,c,...,z]⇒[[z], [y,z], [x,y,z], ... , [a,b, ... ,y,z]] Hint: Slicing is your friend. please write a python program
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 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 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.
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
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that...
LISP Programming Language Write a Bubble Sort program in the LISP Programming Language called “sort” that sorts the array below in ascending order.  LISP is a recursive language so the program will use recursion to sort. Since there will be no loops, you will not need the variables i, j, and temp, but still use the variable name array for the array to be sorted.             Array to be sorted is 34, 56, 4, 10, 77, 51, 93, 30, 5, 52 The...
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....
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...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT