packages feed

digits (empty) → 0.1

raw patch · 5 files changed

+84/−0 lines, 5 filesdep +QuickCheckdep +basebuild-type:Customsetup-changed

Dependencies added: QuickCheck, base

Files

+ LICENSE view
@@ -0,0 +1,11 @@+Copyright (c) 2009, Henry Bucklow+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.+    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.+    * Neither the name of the N-Sim Ltd. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.+
+ Setup.lhs view
@@ -0,0 +1,7 @@+#! /usr/bin/env runhaskell++> import Distribution.Simple+> import System.Cmd+> tests _ _ _ _ = system "runhaskell src/Tests.hs" >> return ()+> main = defaultMainWithHooks (simpleUserHooks {runTests = tests})+
+ digits.cabal view
@@ -0,0 +1,19 @@+Build-Type: Custom+Name: digits+Category: Data+Version: 0.1+Cabal-Version: >= 1.2+Synopsis: Converts integers to lists of digits and back.+Description: Converts integers to lists of digits and back.+License: BSD3+License-File: LICENSE+Copyright: (c) 2009 Henry Bucklow+Author: Henry Bucklow+Maintainer: henry@elsie.org.uk+Tested-With: GHC==6.10+Build-Depends: base >= 3 && < 4, QuickCheck+Exposed-Modules: Data.Digits+Hs-Source-Dirs: src+Extra-Source-Files: src/Tests.hs+GHC-Options: -Wall+
+ src/Data/Digits.hs view
@@ -0,0 +1,36 @@+module Data.Digits (digits, digitsRev, unDigits, prop_digitsRoundTrip) where++import Test.QuickCheck++-- | Returns the digits of a positive integer as a list, in reverse order.+--   This is slightly more efficient than in forward order.+digitsRev :: Integral n+    => n -- ^ The base to use.+    -> n -- ^ The number to convert to digit form.+    -> [n] -- ^ The digits of the number in list form, in reverse.+digitsRev base i = case i of+        0 -> []+        _ -> lastDigit : digitsRev base rest+    where (rest, lastDigit) = quotRem i base++-- | Returns the digits of a positive integer as a list.+digits :: Integral n+    => n -- ^ The base to use (typically 10).+    -> n -- ^ The number to convert to digit form.+    -> [n] -- ^ The digits of the number in list form.+digits base = reverse . digitsRev base++-- | Takes a list of digits, and converts them back into a positive integer.+unDigits :: Integral n+    => n -- ^ The base to use.+    -> [n] -- ^ The digits of the number in list form.+    -> n -- ^ The original number.+unDigits base = foldl (\ a b -> a * base + b) 0++-- | unDigits . digits should be the identity, in any base.+prop_digitsRoundTrip+    :: Integer -- ^ The integer to test.+    -> Integer -- ^ The base to use.+    -> Property+prop_digitsRoundTrip i b = i > 0 ==> b > 1 ==> i == (unDigits b . digits b) i+
+ src/Tests.hs view
@@ -0,0 +1,11 @@+module Main where++import Data.Digits+import Text.Printf+import Test.QuickCheck++tests = [+    ("checkDigitsRoundTrip", quickCheck prop_digitsRoundTrip)]++main = mapM_ (\(s,a) -> printf "%-25s: " s >> a) tests+