Шифрование по цезарю

This commit is contained in:
2024-11-17 19:21:47 +03:00
parent 098db8a7f0
commit ef9f0448e4
2 changed files with 20 additions and 3 deletions

View File

@@ -1,6 +1,7 @@
module Lib
(
createAlphabetFromText
createAlphabetFromText,
encryptCaesar
) where
createAlphabetFromText :: String -> [Char]
@@ -9,4 +10,15 @@ createAlphabetFromText (x:xs)
| x `elem` alphabet = alphabet
| otherwise = x : alphabet
where
alphabet = createAlphabetFromText xs
alphabet = createAlphabetFromText xs
indexOf :: (Eq t) => [t] -> t -> Int
indexOf [] _ = -1
indexOf (x : xs) target
| x == target = 0
| otherwise = 1 + indexOf xs target
encryptCaesar :: [Char] -> Int -> String -> String
encryptCaesar alphabet shift text = map caesarChar text
where
caesarChar c = alphabet !! ((indexOf alphabet c + shift) `mod` length alphabet)