1

i have an assignment in which i have a list of lists , in which every first element of the lists inside the list are always the same, for example: [[element, ....], [element, ...], [element, ...]]. I also have a function that was given to me by the professor, that generates a random list of numbers between 0 and 9 called genRandom. The way it works is you give it an Int such as 5, and you also give it a seed and it gives you [2,6,3,6,8] (for example). What i need to do is, for example, given an Int by the user, let's use 3 for this example, it should return someting like this: [[element, genRandom 2 seed, genRandom 2 seed], [element, genRandom 2 seed, genRandom 2 seed]]. In this example everytime that genRandom 2appears it should appear 2 random numbers generated by the function. This is my attempt so far:

    gerlists :: Int -> Int -> [[Int]]
    gerlists 0 _ = [[]]
    gerlists _ 0 = [[]]
    gerlists n c = replicate n [Reta Terra 0]

In this function I'm creating the list with the first element already defined as I show above.

    addRandomN :: [[Int]] -> Int -> Int -> [[Int]]
    addRandomN (x:xs) c seed = (x ++ addRandomN(genRandom 2 seed): addRandomN(c-1) (genRandom 2 seed)

This is the Function in which I'm trying to concatenate.

I'm so sorry if it's hard to understand what i'm trying to say, english isn't my main language. Thanks in advance :)

1 Answer 1

1

Yes I get the issue, you need several changes, to start, the basis cases of recursion:

genRandom x y = undefined

addRandomN :: [[Int]] -> Int -> Int -> [[Int]]
addRandomN _ 0 _  = []
addRandomN [] _ _ = []
addRandomN (x:xs) c seed = (genRandom 2 seed) : (addRandomN xs (c-1) seed)

with this, the code compiles, and you will be able to fit the empty holes, I don't know the exact type of genRandom, but the function as you are telling, should be approximately something like that

Sign up to request clarification or add additional context in comments.

3 Comments

but the list it receives will be the [[element, randomNumber, randomNumber], etc], won't this give me a list like [[randomNumber, randomNumber, etc], etc]?
@Ramux05 , Yes, add to the question a whole example of input and output algong side with some kind of real data, is hard to understand the question.
well the input on the function addRandomN would be something like [[4 3], [4 3], [4 3]] 2 5 and the output should be something like (for example): [[4 3, 5 6], [4 3, 7 3], [4 3, 6 4]]. if we used 3 instead of 2 on the input, we should get something like: [[4 3, 5 6, 5 4], [4 3, 7 3, 9 3], [4 3, 6 4, 2 1]]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.