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 lgrep that returns the “lines” within a list that contain a given sublist....
Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise to build lgrep. > (lgrep ’(c d e) ’((a b c d e f g) (c d c d e) (a b c d) (h i c d e k) (x y z))) ((a b c d e f g) (c d c d e) (h i c d e k)) You may assume that all...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and...
Question I'm having trouble with: Using LISP, write a recursive function that takes a list and returns the number of times the symbol 'a' occurs in it. Note: do not count a's that might occur in a sublist within the list.
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
Write a function sublist that takes two lists as arguments, and returns true if the first...
Write a function sublist that takes two lists as arguments, and returns true if the first list appears as a contiguous sublist somewhere within the second list, and false otherwise. > (sublist ’(c d e) ’(a b c d e f g)) #t > (sublist ’(a c e) ’(a b c d e f g)) #f Write a function lgrep that returns the “lines” within a list that contain a given sublist. Use the sublist function implemented in previous exercise...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It...
Python Question Using lists, write the function non_unique(list) that takes a list list as argument. It returns a list which duplicated elements remains and each duplicated element is followed by a number which shows how many times it appears. All elements in return list should be in the same order as their appearance in the original list. For example, given the input [‘a’, ‘b’, ‘c’, ‘a’, ‘b’, ‘d’, ‘a’,‘e’], the function would return [‘a’, 3, ‘b’, 2]. Another example, ['abc',...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT