
-- 
-- This file Copyright 2001 Jeffrey B Putnam
-- Please Contact me - jefu@eou.edu - for information 
-- 
hexToInt x 
  | x >= '0' && x <= '9'   = ord x - ord '0' 
  | x >= 'a' && x <= 'f'   = ord x - ord 'a' + 10 
  | x >= 'A' && x <= 'F'   = ord x - ord 'A' + 10   
  | otherwise              = error "hexToInt x out of range"

intToHex x = "0123456789abcdef" !! x 

intToHexWithCarry i c  =    (b,c1) 
                          where 
	                     s = i+c
	                     b = intToHex (s `mod` 16) 
                             c1 = if s > 16 then 1 else 0 

accumulateSum s (l,c) =      (b:l,c')
			where 
			     (b,c1) = intToHexWithCarry s c 
	                 
badd l1 l2 =    lres
             where 
	        lr1     =  (zipWith (+) (map hexToInt l1) (map hexToInt l2)) 
                (lr2,c) = foldr accumulateSum   ([],0) lr1 
                lres = if c == 0 then lr2 else '1':lr2 
 
                