Reputation:
the user has to type the dimension (it's a quadractic matrix) and random letters will be generated. Ex.: generateMatrix 3
[('a', 'f', 'j'), ('t', 'a', 'b'), ('c', 'y', 'k')]
Each tuple is a line
Upvotes: 0
Views: 372
Reputation: 7360
Using tuples this is impossible without manually hardcoding the solution, which is why the easiest is using lists of lists to represent the matrix if the dimension should be variable.
You could then try something like this:
import System.Random
genMatrix n = format n n (take (n*n) (randomRs ('a','z') (mkStdGen (n*n))))
where
format _ 0 _ = []
format n m text = (take n text) : format n (m-1) (drop n text)
genMatrix
gets the number of rows and columns and a "random" string which is then formatted by format
. It just splits the list into parts of length n and conses them onto an empty list.
Example:
*Main> genMatrix 3
["nmq","jfj","oih"]
Upvotes: 1
Reputation: 141
Achieving the randomness you're looking for is no small task for a beginner, but assuming you know the basics of Haskell, LYAH's section on randomness should help you out a lot.
http://learnyouahaskell.com/input-and-output#randomness
In particular, you may find the randomRs
function useful:
ghci> take 10 $ randomRs ('a','z') (mkStdGen 3) :: [Char]
"ndkxbvmomg"
But in order to use that in an application successfully, there's much more you should know about randomness in Haskell and dealing with impurity, so you really should read the whole section.
Another thing to note about your problem is that [(Char, Char, Char)] and [(Char, Char, Char, Char)] are different types, so there's no simple way to have a single function return lists of tuples that are different lengths depending on the input size. For what you're doing, you probably want a list of lists.
Upvotes: 5