-- See programm teeb seljakotiülesannet

-- Baasseljakott ja süsteemi parameetrid

base	:: [Int]
base	= [2,3,7,15,31,60,122,249,509,999]

m,t	:: Int
m	= 2001
t	= 143

-- Funktsioon trans teisendab elemendi x elemendiks x*tegur mod moodul

trans	:: Int -> Int -> Int -> Int

trans tegur moodul x = mod (x*tegur) moodul

-- Krüptititud seljakott

crypsack	::	[Int]	
	
crypsack	= map (trans t m) base 

-- Funktsioon addMarked liidab antud listist need elemendid, mille kohal on
-- teises listis 1d. Kus on 0d, neid ei liida.

addMarked	:: [Int] -> [Int] -> Int

addMarked [] [] = 0

addMarked (x:xs) (y:ys) = x*y + addMarked xs ys

-- Funktsioon numberToCode teeb arvust etteantud pikkusega bitijada

numberToCode	:: Int -> Int -> [Int]

numberToCode nr len = if (length res == len) 
			then res 
			else [0] ++ (numberToCode nr (len-1))
				where res = numberToACode nr

-- numberToCode nr len = [0] ++ numberToCode nr (len-1)

-- Funktsioon numberToACode teeb arvust lihtsalt kahendesituse

numberToACode :: Int -> [Int]

numberToACode 0 = [0]

numberToACode 1 = [1]

numberToACode nr = (numberToACode (div nr 2))++[mod nr 2]

-- Funktsioon codeNumber kodeerib arvu meie süsteemis

codeNumber 	:: Int -> Int

codeNumber x	= addMarked crypsack (numberToCode x 10) 

-- Funktsioon codeToCode teeb kahendkoodist seljakotikoodi

codeToCode	:: String -> Int

codeToCode xs	= addMarked crypsack (stringToIntList xs)

-- Funktsioon stringToIntList teeb kahendstringist vastava 0-1-listi

stringToIntList	:: String -> [Int]

stringToIntList [] = []

stringToIntList ('0':xs) = [0] ++ stringToIntList xs

stringToIntList ('1':xs) = [1] ++ stringToIntList xs

-- Funktsioon charPairCode teeb tähepaarist seljakotikoodi
-- Funktsioon charNumber leiab tähe numbri
-- Funktsioon charBinCode leiab tähe koodilisti
-- Funktsioon charPairBinCode leiab tähepaari koodilisti 

charPairCode	:: (Char,Char) -> Int

charNumber	:: Char -> Int

charBinCode	:: Char -> [Int]

charPairBinCode	:: (Char,Char) -> [Int]

charNumber ch	= if ( (kood >= 65) && (kood <= 90))
			then kood - 64  
			else 0
			where kood = ord ch
			
charBinCode ch 	= numberToCode (charNumber ch) 5

charPairBinCode p	= (charBinCode (fst p)) ++ (charBinCode (snd p))

charPairCode p	= addMarked crypsack (charPairBinCode p)

