aydarbiktimirov

Zhegalkin polynomial by truth table

Jun 13th, 2011
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.List (intercalate)
  2.  
  3. xor a b = (a || b) && not (a && b)
  4.  
  5. triangle [] = []
  6. triangle l@(x:xs) = [l] ++ (triangle $ zipWith xor l xs)
  7.  
  8. num2string _ _ 0 = "1"
  9. num2string result var n
  10.     | mod n 2 == 0 = num2string result (pred var) $ div n 2
  11.     | n == 1 = (var:result)
  12.     | otherwise = num2string (var:result) (pred var) $ div n 2
  13.  
  14. letter x n
  15.     | n <= 1 = x
  16.     | otherwise = letter (succ x) (pred n)
  17.  
  18. zhegalkin [] = []
  19. zhegalkin l
  20.     | l == [] = "1"
  21.     | (length l) /= 2 ^ varCount = error "Incorrect list length"
  22.     | otherwise = intercalate "+" $ filter (/= "") $ map stringFactor $ zip (map head (triangle l)) [0..]
  23.     where   stringFactor x
  24.             | fst x = num2string "" (letter 'A' varCount) $ snd x
  25.             | otherwise = []
  26.         varCount = truncate $ logBase 2 $ fromIntegral $ length l
Advertisement
Add Comment
Please, Sign In to add comment