haskell's a bitch
first time programming , and i'm given tutorial exercises to which I have no idea how to complete. eugh.
Using the function rotate from the previous question, write a function
makeKey :: Int -> [(Char, Char)]
that returns the cipher key with the given offset. See above for the description of how the cipher key is represented as a list of pairs. Example:
Main> makeKey 5 [(’A’,’F’),(’B’,’G’),(’C’,’H’),(’D’,’I’),(’E’,’J’),(’F’,’K’), (’G’,’L’),(’H’,’M’),(’I’,’N’),(’J’,’O’),(’K’,’P’),(’L’,’Q’), (’M’,’R’),(’N’,’S’),(’O’,’T’),(’P’,’U’),(’Q’,’V’),(’R’,’W’), (’S’,’X’),(’T’,’Y’),(’U’,’Z’),(’V’,’A’),(’W’,’B’),(’X’,’C’), (’Y’,’D’),(’Z’,’E’)]
The cipher key should show how to encrypt all of the uppercase English letters, and there should be no duplicates: each letter should appear just once amongst the pairs’ first compo- nents (and just once amongst the second components).
i mean seriously what the hell
On the other hand...if you don't give up, you should learn certain concepts much faster
I don't know Haskell, but here's a JavaScript solution in case that helps
Click for JSFiddle
Edit: Looks like you've already got a
rotate
function from a previous exercise, with that method the general process would be something like this:1. Get list of characters from A-Z:
['A'..'Z']
2. You want to create pairs for each item in the list:
('A', rotate 'A' 5)
3. So, you have the list and the method that converts each item in the list into the result you want. This is called a
map
, or I think the Haskell term is `list comprehension`:[(x, rotate x 5) | x <- ['A'..'Z']]
4. Put it in a function , change 5 to an input parameter:
makeKey n = [(x, rotate x n) | x <- ['A'..'Z']]
As I said I don't know Haskell so sorry if this is misleading or incorrect! Also I'm not passing your course for you so don't expect me to give you all your answers
I'm sure you've already come across this but I hear Learn You A Haskell is a great way to learn the language.
the real issue was I had no idea how to do rotate, to which after some thinking I got
rotate :: Int -> [Char] -> [Char]
rotate a xs | a >= 1 && a <= (length xs) = (drop a xs) ++ (take a xs) to which I could do
makeKey :: Int -> [(Char, Char)]
makeKey rotateChar = zip ['A'..'Z'] (rotate rotateChar ['A'..'Z'])
there's some other stuff I can't do but I guess that's what these tutorial sessions are for.
It's also apparently becoming more prelevant as haskell is very good with multi-core processors and parallel computing, so it's faster than other languages.
but it's also a bitch and nobody uses it
That concept of the 'infinite list' is called a
generator
and exists in many procedural and OO languages like Python (2.3), C# (2.0), JavaScript (1.7), PHP (5.5), and many more. It's certainly not exclusive to Haskell by any means.Something like this is quite valid in C#:
IEnumerable<int> Numbers()
{
int i = 1;
while(true) yield return i++;
}
var five = Numbers().Take(5); // Contains [1, 2, 3, 4, 5]