Guest User

Untitled

a guest
Jan 21st, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- Informatics 1 - Functional Programming
  2. -- Tutorial 2
  3. --
  4. -- Week 4 - due: 13/14 Oct.
  5.  
  6. import Data.Char
  7. import Data.List
  8. import Test.QuickCheck
  9.  
  10.  
  11. -- 1.
  12. rotate :: Int -> [Char] -> [Char]
  13. rotate n []                          = error "Cannot rotate empty list"
  14. rotate n xs |n <= length xs && n > 0 = drop n xs ++ take n xs
  15.             |n <= 0                  = error "You cannot rotate a string by 0 or by a negative number."
  16.             |n > length xs           = error "n cannot be bigger than your string."
  17.  
  18.  
  19.  
  20. -- 2.
  21. -- (b) It takes the modulus of the number by the length of the string
  22. -- if it is too large or negative it will return 0, which will end
  23. -- in doing rotate l str == str which is true.
  24.  
  25. prop_rotate :: Int -> String -> Bool
  26. prop_rotate k str = rotate (l - m) (rotate m str) == str
  27.                         where l = length str
  28.                               m = if l == 0 then 0 else k `mod` l
  29.  
  30. -- 3.
  31.  
  32.  
  33. makeKey :: Int -> [(Char, Char)]
  34. makeKey n  = zip ['A'..'Z'] (rotate n ['A'..'Z'])
  35.            
  36.  
  37. -- 4.
  38. -- unzip the 2 lists, try if k is equal to the first term of my list
  39. -- if not drop the first
  40. lookUp :: Char -> [(Char, Char)] -> Char
  41. lookup c []                 = c
  42. lookUp c  ((x,y):xs) | c==x = y
  43.                      | c/=x = lookUp c xs
  44.                          
  45.  
  46. -- 5.
  47. encipher :: Int -> Char -> Char
  48. encipher n c = lookUp c (makeKey n)
  49.  
  50. -- 6.
  51. normalize :: String -> String
  52. normalize (xs)  = [toUpper x | x <- xs, isAlpha x || isDigit x]
  53.  
  54. -- 7.
  55. encipherStr :: Int -> String -> String
  56. encipherStr = undefined
  57.  
  58. -- 8.
  59. reverseKey :: [(Char, Char)] -> [(Char, Char)]
  60. reverseKey [] = []
  61. reverseKey ((x,y):xs) = (y,x) : reverseKey (xs)
  62.  
  63. -- 9.
  64. decipher :: Int -> Char -> Char
  65. decipher = undefined
  66.  
  67. decipherStr :: Int -> String -> String
  68. decipherStr = undefined
  69.  
  70. -- 10.
  71. prop_cipher :: Int -> String -> Property
  72. prop_cipher = undefined
  73.  
  74. -- 11.
  75. contains :: String -> String -> Bool
  76. contains = undefined
  77.  
  78. -- 12.
  79. candidates :: String -> [(Int, String)]
  80. candidates = undefined
  81.  
  82.  
  83.  
  84. -- Optional Material
  85.  
  86. -- 13.
  87. splitEachFive :: String -> [String]
  88. splitEachFive = undefined
  89.  
  90. -- 14.
  91. prop_transpose :: String -> Bool
  92. prop_transpose = undefined
  93.  
  94. -- 15.
  95. encrypt :: Int -> String -> String
  96. encrypt = undefined
  97.  
  98. -- 16.
  99. decrypt :: Int -> String -> String
  100. decrypt = undefined
  101.  
  102. -- Challenge (Optional)
  103.  
  104. -- 17.
  105. countFreqs :: String -> [(Char, Int)]
  106. countFreqs = undefined
  107.  
  108. -- 18
  109. freqDecipher :: String -> [String]
  110. freqDecipher = undefined
Add Comment
Please, Sign In to add comment