]> gitweb.michael.orlitzky.com - spline3.git/blobdiff - src/RealFunction.hs
Whitespace cleanup and hlint fix in RealFunction.
[spline3.git] / src / RealFunction.hs
index 6a557a9f0f4d4bdb33df42485d84ae02e57cabe5..307a0f0c5cb9596a58d8c6375ee9b06fbd79266c 100644 (file)
@@ -1,6 +1,9 @@
-{-# LANGUAGE TypeSynonymInstances #-}
+{-# LANGUAGE FlexibleInstances #-}
 
-module RealFunction
+module RealFunction (
+  RealFunction,
+  cmult,
+  fexp )
 where
 
 
@@ -11,13 +14,13 @@ type RealFunction a = (a -> Double)
 instance Show (RealFunction a) where
     -- | There is nothing of value that we can display about a
     --   function, so simply print its type.
-    show _ = "RealFunction"
+    show _ = "<RealFunction>"
 
 
 -- | An 'Eq' instance is required to be a 'Num' instance.
 instance Eq (RealFunction a) where
     -- | Nothing else makes sense here; always return 'False'.
-    _ == _ = False
+    _ == _ = error "You can't compare functions for equality."
 
 
 -- | The 'Num' instance for RealFunction allows us to perform
@@ -34,13 +37,38 @@ instance Num (RealFunction a) where
 
 -- | Takes a constant, and a function as arguments. Returns a new
 --   function representing the original function times the constant.
+--
+--   ==== __Examples__
+--
+--   >>> let square x = x**2
+--   >>> square 1
+--   1.0
+--   >>> square 2
+--   4.0
+--   >>> let f = cmult 2 square
+--   >>> f 1
+--   2.0
+--   >>> f 2
+--   8.0
+--
 cmult :: Double -> (RealFunction a) -> (RealFunction a)
 cmult coeff f = (*coeff) . f
 
 
--- | Takes a function f and an exponent n. Returns a new function,
---   f^n.
+-- | Takes a function @f@ and an exponent @n@. Returns a new function,
+--   @g@, defined by g(x) = (f(x))^n. This is /not/ @f@ composed
+--   with itself @n@ times.
+--
+--   ==== __Examples__
+--
+--   >>> let square x = x**2
+--   >>> square 2
+--   4.0
+--   >>> let f = fexp square 3
+--   >>> f 2
+--   64.0
+--
 fexp :: (RealFunction a) -> Int -> (RealFunction a)
 fexp f n
-     | n == 0 = (\_ -> 1)
+     | n == 0 = const 1
      | otherwise = \x -> (f x)^n