gray-extended (empty) → 1.2
raw patch · 5 files changed
+132/−0 lines, 5 filesdep +QuickCheckdep +basedep +base-unicode-symbolssetup-changed
Dependencies added: QuickCheck, base, base-unicode-symbols, gray-extended, test-framework, test-framework-quickcheck2
Files
- LICENSE +27/−0
- Setup.hs +2/−0
- gray-extended.cabal +37/−0
- src/Codec/Gray.hs +51/−0
- test/Main.hs +15/−0
+ LICENSE view
@@ -0,0 +1,27 @@+Copyright (c) 2010-2012, Amy de Buitléir+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 author nor the names of other 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 +HOLDER 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.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ gray-extended.cabal view
@@ -0,0 +1,37 @@+name: gray-extended+version: 1.2+synopsis: Gray encoding schemes+description: Gray codes satisfy the property that two successive values + differ in only one digit. Usually the term \"Gray code\"+ refers to the Binary Reflected Gray code (BRGC), but + non-binary Gray codes have also been discovered.+category: Math+Homepage: https://github.com/mhwombat/gray-extended+cabal-version: >=1.8+build-type: Simple+author: Amy de Buitléir+copyright: (c) Amy de Buitléir 2010-2012+license: BSD3+stability: experimental+maintainer: amy@nualeargais.ie+license-file: LICENSE++library+ hs-source-dirs: src+ build-depends: base ==4.*,+ base-unicode-symbols ==0.2.*+ ghc-options: -Wall+ exposed-modules: Codec.Gray++test-suite gray-extended-tests+ type: exitcode-stdio-1.0+ build-depends: base ==4.*,+ test-framework-quickcheck2 == 0.2.*,+ QuickCheck == 2.4.*,+ test-framework == 0.*,+ base-unicode-symbols ==0.2.*,+ gray-extended+ hs-source-dirs: test+ ghc-options: -Wall -Werror -rtsopts+ main-is: Main.hs+
+ src/Codec/Gray.hs view
@@ -0,0 +1,51 @@+-----------------------------------------------------------------------------+-- |+-- Module : Codec.Gray+-- Copyright : (c) Amy de Buitléir 2011-2012+-- License : BSD-style+-- Maintainer : amy@nualeargais.ie+-- Stability : experimental+-- Portability : portable+--+-- Gray encoding schemes. A Gray code is a list of values such that two+-- successive values differ in only one digit. Usually the term /Gray code/+-- refers to the Binary Reflected Gray code (BRGC), but non-binary Gray codes+-- have also been discovered. Some Gray codes are also /cyclic/: the last and+-- first values differ in only one digit.+--+-----------------------------------------------------------------------------+{-# LANGUAGE UnicodeSyntax #-}++module Codec.Gray + (+ grayCodes,+ naryGrayCodes+ ) where++import Data.List (foldl')++-- | @'grayCodes' k@ generates the list of Binary Reflected Gray Code (BRGC)+-- numbers of length k. This code is cyclic.+grayCodes ∷ Int → [[Bool]]+grayCodes 0 = [[]]+grayCodes k = + let xs = grayCodes (k-1) in map (False:) xs ++ map (True:) (reverse xs)++-- | @'naryGrayCodes' xs k@ generates a non-Boolean (or n-ary) Gray code of+-- length @k@ using the elements of @x@ as "digits". This code is cyclic.+--+-- Ex: @'naryGrayCodes' "012" 4@ generates a ternary Gray code that is +-- four digits long.+naryGrayCodes ∷ [a] → Int → [[a]]+naryGrayCodes xs 1 = map (\x → [x]) xs+naryGrayCodes xs k = snd $ foldl' prefixAndShift (ys,[]) xs'+ where ys = naryGrayCodes xs 1+ xs' = naryGrayCodes xs (k-1)++-- | Shift elements right.+shift ∷ [a] → [a]+shift as = last as : init as++prefixAndShift ∷ ([[a]],[[a]]) → [a] → ([[a]],[[a]])+prefixAndShift (ys,zs) xs = (shift ys, zs ++ (map (xs++) ys))+
+ test/Main.hs view
@@ -0,0 +1,15 @@+{-# LANGUAGE UnicodeSyntax #-}+module Main where++import Codec.GrayQC ( test )++import Test.Framework as TF ( defaultMain, Test )++tests ∷ [TF.Test]+tests = + [ + Codec.GrayQC.test+ ]++main ∷ IO ()+main = defaultMain tests