Question
IN HASKELL: 1. Give all pairs where character occurs in code. Examples: *Main> pairFirst code1 'a' [('a','z')] *Main> pairFirst code2 'a' [('a','a'),('a','c')] 2. Encrypt a
IN HASKELL:
1. Give all pairs where character occurs in code. Examples:
*Main> pairFirst code1 'a'
[('a','z')]
*Main> pairFirst code2 'a'
[('a','a'),('a','c')]
2. Encrypt a character using a code. If no mapping for a character exists, it encrypts to itself. If more than one mapping exists, just use the first value. Examples:
*Main> encryptChar code1 'a'
'z'
*Main> encryptChar code2 's'
's'
*Main> encryptChar code2 'd'
'd'
3. Encrypt a string using a code. Examples:
*Main> encryptString code1 "Hello world!"
"Hvool dliow!"
*Main> encryptString code2 "Hello world!"
"Hello world!"
------------------------------------
Given:
module TestQ1 (pairFirst,encryptChar,encryptString,howManyValues,numInvalid,distinctMap,ownInverse,subset,allMapped,mapLetters) where
-- Code is a type synonym -- it says that a Code is a list of Pairs of Chars type Code = [(Char,Char)]
-- domain of our code domain1 :: [Char] domain1 = ['a'..'z'] domain2 = ['a','b','a']
-- associated range range1 :: [Char] range1 = ['z','y'..'a'] range2 = ['a','c','c']
-- Turns two strings into a code makeCode :: [Char] -> [Char] -> Code makeCode domain range = zip domain range
-- create a code out of our domain and range -- I will call each pair a mapping from the first element of the pair to the second code1 :: Code code1 = makeCode domain1 range1 code2 = makeCode domain2 range2
-- pairFirst takes a Code and Char -- returns the list of all Pairs which have that Char as first element pairFirst :: Code -> Char -> Code pairFirst code ch -- Question 1 for homework -- I have given a definition here just so it type checks -- You need to replace this with the proper definition = code
-- uses a code to encrypt a Char -- if the Char has no mapping then the Char encrypts as itself -- if more than one mapping just use the first value encryptChar :: Code -> Char -> Char encryptChar code ch -- Question 2 for homework -- I have given a definition here just so it type checks -- You need to replace this with the proper definition = ch
-- uses a Code to encrypt a String encryptString :: Code -> String -> String encryptString code chars -- Question 3 for homework -- I have given a definition here just so it type checks -- You need to replace this with the proper definition = chars
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started