// @(#)$Id: NumericCodeProblem.scala,v 1.1 2005/10/28 09:03:35 leavens Exp leavens $
package scalahw;
import Stream._;
trait NumericCodeProblem {
  /** Iteration of a function, starting with a given argument. */
  def iterate[T](f: T => T)(x:T): Stream[T] = cons(x, iterate(f)(f(x)));

  /** Next approximation for square root compuation. */
  def next(n: Double)(x: Double) = (x + n / x) / 2.0;

  /** Sequence of square root approximations */    
  def approximations(n: Double): Double => Stream[Double];

  /** Find an approximation within epsilon. */    
  def within(epsilon: Double)(str: Stream[Double]): Double;

  /** Compuate square roots to within epsilon. */  
  def squareRoot(a0: Double)(epsilon: Double)(n: Double): Double;
  
  /** Infinite list of doubles, starting at n.
   * (Adapted from Scala by Example, by Odersky) */
  def from(n: Double): Stream[Double] = cons(n, from(n + 1.0));
}
