practical-language-learning

exercise-9

  1. Define a function that takes…
  2. Define a function that takes…
  3. A faraway planet is inhabited…
  4. Define a function that takes 8…
  5. Suppose f is a function of one…
  6. Horner’s method is a trick for…
  7. How many bits would you estimate…
  8. How many distinct types of float does your implementation provide?

Define a function that takes…

1.Define a function that takes a list of reals and returns true iff they are in nondecreasing order.

(defun non-decrease (lst)
  (if (null (cdr lst))
      t
      (and (<= (car lst) (cadr lst)) (non-decrease (cdr lst)))))

Define a function that takes…

2.Define a function that takes an integer number of cents and returns four values showing how to make that number out of 25-, 10-, 5- and 1-cent pieces, using the smallest total number of coins.

(defun total-cents (cents)
  (total-centss cents '(25 10 5 1)))
(defun total-centss (cents lst)
  (if (null lst)
      nil
      (multiple-value-bind (x y) (floor cents (car lst))
        (cons x (total-centss y (cdr lst))))))

TODO A faraway planet is inhabited…

3.A faraway planet is inhabited by two kinds of beings, wigglies and wobblies. Wigglies and wobblies are equally good at singing. Every year there is a great competition to chooses the ten best singers. Here are the results for the past ten years:

YEAR 1 2 3 4 5 6 7 8 9 10
WIGGLIES 6 5 6 4 5 5 4 5 6 5
WOBBLIES 4 5 4 6 5 5 6 5 4 5

Write a program to simulate such a contest. Do your results suggest that the committee is, in fact, choosing the ten best singers each year?

Define a function that takes 8…

4.Define a function that takes 8 reals representing the endpoints of two segments in 2-space, and returns either nil if the segments do not intersect, or two values representing the x- and y-coordinates of the intersection if they do.

;;; solution origin: https://stackoverflow.com/questions/9043805/test-if-two-lines-intersect-javascript-function

(defun intersect-line (l1 l2 r1 r2)
  (intersects
   (car l1) (cdr l1) (car l2) (cdr l2)
   (car r1) (cdr r1) (car r2) (cdr r2)))

(defun intersects (a b c d p q r s)
  (let* ((det (- (* (- c a) (- s q))
                 (* (- r p) (- d b))))
         (gamma (/ (+ (* (- s q) (- r a))
                      (* (- p r) (- s b)))
                   det))
         (lambdap (/ (+ (* (- b d) (- r a))
                        (* (- c a) (- s b)))
                     det)))
    (cond ((= det 0) nil)
          ((and (and (> lambdap 0) (< lambdap 1))
                (and (> gamma 0) (< gamma 1)))
           (cons (+ a (* lambdap (- c a)))
                 (+ b (* lambdap (- d  b)))))
          (t nil))))

;; (intersect-line '(1 . 4) '(3 . 2) '(2 . 2) '(4 . 4))

TODO Suppose f is a function of one…

5.Suppose f is a function of one (real) argument, and that min and max are nonzero reals with different signs such that f has a root (returns zero) for one argument i such that min < i < max. Define a function that takes four arguments, f, min, max, and epsilon, and returns an approximation of i accurate to within plus or minus epsilon.

(defun approximat-root (fn min max eps)
  ())

;; (approximat-root #'+ -2 2 .5)

TODO Horner’s method is a trick for…

6 Horner’s method is a trick for evaluating polynomials efficiently. To find ax^3+bx^2+cx+d you evaluate x(x(ax+b)+c)+d. Define a function that takes one or more arguments—the value of x followed by n reals representing the coefficients of an (n - l)th-degree polynomial—and calculates the value of the polynomial by Horner’s method.

How many bits would you estimate…

7.How many bits would you estimate your implementation uses to represent fixnums?

(values most-positive-fixnum most-negative-fixnum)

How many distinct types of float does your implementation provide?

Four types: short-float, single-float, double-float, long-float