;;;-*- Mode: LISP; Syntax: Common-Lisp; Base: 10; Package: COMMON-LISP-USER -*- ;;; $Id: solut1.lisp,v 1.1 1995/10/03 16:27:11 lyman Exp lyman $ ;;; $Revision: 1.1 $ ;;;; Overview ;;; ;;; Initial Author: Lyman Taylor ( lyman@cc.gatech.edu ) ;;; For: 2360 Fall 95 ;;; ;;; Modification: ;;; See RCS Log File. ;;; ;;; This file is a solution for assignment 1. It contains the four prescribed ;;; functions and their associated helper functions. ;;; ;;; This file has 3 major sections after this Overview. ;;; ;;; Section 1. Questions 1-4 ;;; ;;; Section 2. Misc. fcns. ;;; ;;; Section 3. Dribbled results. ;;; ;;; ;;;; Questions 1-4 ;;; Question 1. ;;; (defun era ( runs innings-pitched ) "Calculate a ERA, earn run average, given runs given up and the number of innings pitched. WARNING: if latter is zero an error will result. " (* (runs-per-inning runs innings-pitched ) 9.0 ) ) (defun runs-per-inning ( runs innings ) (/ runs innings )) ;;; Question 2 ;;; (defun circle-stuff ( radius ) "Return a list of the circle's radius , area, and circumference given a radius." (list radius (circle-area radius ) (circle-circumference radius ) ) ) (defun circle-area ( radius ) "Returns the area of a circle." (* pi (square radius )) ) (defun circle-circumference ( radius ) "Returns the circumference of a circle. " (* 2 pi radius ) ) ;;; Question 3 ;;; (defun hypotenuse ( side-1 side-2 ) "Returns the length of the hypotenuse of a right triangle given the length of the two sides." (sqrt (sum-squares side-1 side-2 ) ) ) (defun sum-squares ( number-1 number-2 ) "Return the sum of the squares of two nubmers." (+ (square number-1 ) (square number-2 ))) ;;; Question 4 #| The roots of the quadratic equation are found by: ________ / 2 -b +- \/ b - 4ac x = _________________ 2a |# (defun quadratic ( a b c ) "Return a list of the quadratic equation's two roots given the coefficients a, b, and c. " (list (pos-root a b c ) (neg-root a b c ) )) (defun pos-root ( a b c ) (/ (pos-numerator a b c ) (my-denominator a ))) (defun neg-root ( a b c ) (/ (neg-numerator a b c ) (my-denominator a ))) (defun my-denominator ( a ) (* 2 a ) ) (defun pos-numerator ( a b c ) ( + (- b ) (discriminant a b c))) (defun neg-numerator ( a b c) ( - (- b ) (discriminant a b c))) (defun discriminant ( a b c ) (sqrt (- (square b ) (* 4 a c ) ))) ;;;; Misc. Fcns. (defun square ( number ) " Return the square of the given number." (* number number )) ;;; Dribbled Results #| These results are actually cut and pasted out of the Lispworks listener... CL-USER 2 > (era 1 9 ) 1.0 CL-USER 3 > (era 9 1 ) 81.0 CL-USER 8 > (circle-stuff 4 ) (4 50.26548245743669D0 25.132741228718345D0) CL-USER 14 > (hypotenuse 3 4 ) 5.0 CL-USER 17 > (quadratic 2 0 2 ) (#C(0.0 1.0) #C(0.0 -1.0)) CL-USER 18 > (quadratic 2 2 0 ) (0.0 -1.0) |# ;;; EOF ;;;;;;;