Table of Contents
1 p Exercise 1.1 [m]
Define a version of 1a s t -name that handles "Rex Morgan MD," "Morton Downey, Jr.," and whatever other cases you can think of.
(setf name1 '(Rex Morgan MD\,)) (setf name2 '(Morton Downey\, Jr\.\,)) (defun last-name(name) "last name" (first (last name))) (last-name name1)
2 p Exercise 1.2 [m]
Write a function to exponentiate, or raise a number to an integer power. For example: (power 3 2 ) = 32 = 9.
(defun power(num mul-times) "power a num" (if (equal mul-times 0) 1 (* num (power num (- mul-times 1))))) (power 3 4)
3 p Exercise 1.3[m]
Write a function that counts the number of atoms in an expression. For example: ( count - a toms ' ( a ( b ) c ) ) = 3. Notice that there is something of an ambiguity in this: should ( a ni 1 c ) count as three atoms, or as two, because it is equivalent to ( a nil c ) ?
(defun count-atoms(obj) "count atoms in obj exclude 'nil '() " (if (typep obj 'list) (apply #'+ (mapcar #'count-atoms obj)) 1))
4 Exercise 1.4 [m]
Write a function that counts the number of times an expression occurs anywhere within another expression. Example: ( count - anywhere ' a ' ( a ( ( a ) b ) a ) ) 3.
(defun count-anywhere (item tree) "count the times item appears anywhere within tree" (cond ((eql item tree) 1) ((atom tree) 0) (t (+ (count-anywhere item (first tree)) (count-anywhere item (rest tree))))))
5 Exercise 1.5 [m]
Write a function to compute the dot product of two sequences of numbers, represented as lists. The dot product is computed by multiplying corresponding elements and then adding up the resulting products. Example:
(dot-product '(10 20) ' ( 3 4 ) ) = 10 x 3 + 20 x 4 = 110
(defun dot-product(f-list s-list) "dot product" (apply #'+ (mapcar #'* f-list s-list))) (dot-product '(10 20) '(3 4))
Render by hexo-renderer-org with Emacs 25.3.1 (Org mode 9.1.1)