Write a function DOT-PRODUCT that takes two lists, each list has three numbers (
ID: 3555679 • Letter: W
Question
Write a function DOT-PRODUCT that takes two lists, each list has three numbers (elements), and produces the dot product of the vectors that they represent. For example,
-> (DOT-PRODUCT '(1.2 2.0 -0.2) '(0.0 2.3 5.0))
-> 3.6
Write a function COUNT-NUMBER that counts the number of numbers that occur in a list. It should work like this:
-> (COUNT-NUMBER '(A 2.3 B 5 4.53 C F))
-> 3
Write a function NEW-LIST that takes a number as its argument and constructs a list of that length containing all Ts. For example,
-> (new-lit 5)
-> (T T T T T)
The Lisp function LENGTH counts the number of elements in the top level of a list. Write a function ALL-LENGTH of one argument that counts the number of atoms that occur in a list at all levels. Thus, the following lists will have the following ALL-LENGTHs.
(A B C) => 3
((A (B C) D (E F)) => 6
(NIL NIL (NIL NIL) NIL ) => 5
Explanation / Answer
I redefine the length function (Not asked as far as your question requires though)
(defun length (l)
(if (null l)
0
(+ 1 (length (cdr l)))))
DOT-PRODUCT:
(defun dot-product (l1 l2)
(if (and (null l1) (null l2))
0
(+ (* (car l1) (car l2)) (dot-product (cdr l1) (cdr l2)))))
COUNT-NUMBER:
(defun count-number (l)
(if (null l) 0 (+ (if (numberp (car l)) 1 0) (count-number (cdr l)))))
NEW-LIST:
(defun new-list (n)
(if (= n 0) '() (cons T (new-list (- n 1)))))
ALL-LENGTH:
(defun all-length (l)
(if (= 0 (length l)) 0 (+ (if (listp (car l)) (if (equal NIL (car l)) 1 (all-length (car l))) 1) (all-length (cdr l)))))
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.