;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; An example Lisp file for CSE 3401 reports.
;
; The first part of the file is the definition of the functions together with
; documentation as Lisp comments; for example documentation see
; functionals.lsp.  You can leave space to draw diagrams on the listing when
; is printed.
;
; The second part of the file is a series of calls to print-test.
; The file exercise-N.tests is produced when the following command is
; executed on Prism.
;
;      clisp exercise-N.lsp >! exercise-N.tests
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(load 'functionals.lsp)
(load 'functionals-base.lsp)
(load 'print-test.lsp)

;; There is a contradiction in the specification.  [ASIDE: resolve the
;; contradiction and document your choice;  documentation is vital.]
;; There are two possible interpretations of "term", which are shown in the
;; following.
;;
;; Choice 1:  sine(x, 0) = x.
;; Choice 2:  use  2*n - 1 in the last term.
;;
;; In the given solution I made choice 2, so that n would mean the number of
;; terms, which makes it easier to use the function.

;; Require: x is any number and n is the number of terms of the sine series
;;          to compute
;; Ensure: Result is the sum of n terms of the sine series using x as the value.

(defun sine-sigma (x n)
  (sigma 1 n  (bu 'one-term x)))

;; Require: x is any number and i represents the i'th term of the sine series,
;;          where counting begins at zero.
;; Ensure: Result is -1**(i+1) * x**(2i-1)/(2i-1)!

(defun one-term (x i)
 (* (cond ((evenp i) -1.0)
             (t 1.0))
    (/  (power x (1- (+ i i)))
        (factorial (1- (+ i i))))
))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Test cases for the functions.
; Note in tests 2 and 3 the comment goes across multiple lines, which shows
; that you can have longer comments when needed.

(princ "Tests for exercise N")

(print-test "Test 1: Compute 1 term: "
 '(sine-sigma 3.0 1))
 
(print-test "Test 2: Compute
     2 terms "
 '(sine-sigma 3.0 2))

(print-test "Test 3: Compute
    many
    terms

    "
 '(sine-sigma 3.0 5))
