From d11ba3404aadbb2a81da295bea4ac53217703de3 Mon Sep 17 00:00:00 2001 From: stla Date: Sat, 4 May 2024 12:41:48 +0200 Subject: [PATCH] signAnalysis --- scripts/signAnalysis.hs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/scripts/signAnalysis.hs b/scripts/signAnalysis.hs index fb69cfa..b251df0 100644 --- a/scripts/signAnalysis.hs +++ b/scripts/signAnalysis.hs @@ -1,4 +1,6 @@ +{-# LANGUAGE FlexibleContexts #-} import qualified Algebra.Absolute as AlgAbs +import qualified Algebra.Additive as AlgAdd import qualified Algebra.Ring as AlgRing runLengthEncoding :: Eq a => [a] -> [(a,Int)] @@ -9,8 +11,8 @@ runLengthEncoding = foldr code [] | c == x = (x,n+1):ts | otherwise = (c,1):(x,n):ts -signPermancesAndVariations :: (Eq a, AlgRing.C a, AlgAbs.C a) -> [a] -> (Int, Int) -signPermancesAndVariations as = (permances, variations) +signPermancesAndVariations :: (Eq a, AlgRing.C a, AlgAbs.C a) => [a] -> (Int, Int) +signPermancesAndVariations as = (permanences, variations) where sign a = if AlgAbs.signum a == AlgRing.one then '+' else '-' signs = map sign as @@ -20,15 +22,16 @@ signPermancesAndVariations as = (permances, variations) permanences = sum lengths - l variations = l - 1 -signVariations' :: (Eq a, AlgRing.C a, AlgAbs.C a) -> [a] -> Int +signVariations' :: forall a. (Eq a, AlgRing.C a, AlgAbs.C a) => [a] -> Int signVariations' as = v1 + v2 + 2*v3 where count x xs = sum (map (fromEnum . (== x)) xs) l = length as - sign a = case AlgAbs.signum a of - AlgRing.zero -> '0' - AlgRing.one -> '+' - _ -> '-' + sign :: a -> Char + sign a + | AlgAbs.signum a == AlgAdd.zero = '0' + | AlgAbs.signum a == AlgRing.one = '+' + | otherwise = '-' signs = map sign as chunks2 = [(signs !! i, signs !! (i+1)) | i <- [0 .. l-2]] v1 = count ('+', '-') chunks2