1. Write a function vecmul that will take as inputs two simple lists of numbers. vecmul should multiply these lists element-wise as one would multiply vectors. Assume the two lists are of the same length. [For example, (vecmul '(2 3 4 5) '(1 4 5 2)) returns (2*1 3*4 4*5 5*2) or (2 12 20 10).] (5 Marks)
2. Define function addvec and use vecmul to define a product function innpuch to find sum of the products, such as, (innprod '(1 2 3) '(7 8 9)) returns 1*7 + 2*8 + 3*9 or 50. (5 Marks)
3. Define a function last1 which returns the last element of a list (at the top level) without making use of reverse or last (already built in to Common Lisp.) (5 Marks)
4. Write a reverse function srev which reverses all lists that occur in an S-expression at any level. Thus (srev '((a b (c d)) e (f g))) should yield ((g f) e ((d c) b a)).
(5 Marks)
5. Write a function insert1 which takes its first integer argument and inserts it at the correct place in its second argument, a list of integers sorted in increasing order, so that (insert1 3 '(1 4 6 6 7)) produces (1 3 4 6 6 7).
(5 Marks)
6. Use insert to write a function sort1 which sorts a list of integers into increasing order. [We are done if the list is nil. Otherwise insert the car of the list into the sorted cdr]. (5 Marks)
7. Define a function occurrence that takes a list and returns a list indicating the number of times each (eql) element appears, sorted from most common element to least common.
(5 Marks)
Example:(occurrences '(a b a d a c d c a)) ?
((A 4) (C 2) (D 2) (B 1))
8. Your friend is trying to write a function that returns the sum of all the non-nil elements in a list to help you to submit assignment on your behalf. He has written two versions of this function and neither of them work. Explain what is wrong with each, and give a correct version.
(2?5 Marks)
(1 nil 4 5 nil 8 9 nil nil) => 27
(a) (defun summit (lst)
(remove nil lst)
(apply #'+ lst))
(b) (defun summit (lst)
(let ((x (car lst)))
(if (null x)
(summit (cdr lst))
(+ x (summit (cdr lst))))))
9. Suppose the function pos+ takes a list and returns a list of each element plus its position. (10 Marks)
(pos+ '(7 5 1 4))? (7 6 3 7)
Define this function using (a) recursion (b) iteration.