Haskell实现嵌套列表添加genRandom生成随机元素功能求助
Hey there! Let's work through this problem step by step. First, let's make sure we're aligned on your requirements:
Your Core Requirements
- Generate a nested list where each sublist starts with a fixed element (like
elementin your example) - The top-level list should have a number of sublists equal to the user-provided Int input (e.g., input 3 gives 3 sublists)
- For each sublist, after the fixed element, append results from multiple calls to
genRandom 2 seed(each call returns a 2-element list of 0-9 random numbers) - Ensure each call to
genRandomuses a unique seed to avoid duplicate random sequences
Issues in Your Current Code
Let's break down the problems with your existing code:
- Typo & Base Case Errors:
gerlistshas a typo (likelygenLists), and your base cases return[[]]—when the input count is 0, we should return an empty list[]instead of a list containing an empty list. - Syntax & Logic Flaws in
addRandomN: The function has mismatched parentheses, incorrect recursion structure, and tries to pass a[Int](result ofgenRandom) to a function expecting[[Int]]. - Seed Handling: Using the same seed for every
genRandomcall will produce identical random lists—we need to chain seeds to get unique sequences.
Solution Code
First, let's define our fixed element (replace this with your actual fixed value):
fixedElement :: Int fixedElement = 0 -- Swap this with your specific fixed element, e.g., the value from "Reta Terra 0"
Next, a helper to generate the base list of sublists with just the fixed element:
genBaseLists :: Int -> [[Int]] genBaseLists 0 = [] -- Return empty list when count is 0 genBaseLists n = replicate n [fixedElement]
Assuming your professor's genRandom follows standard pure random number generation (returns both the random list and a new seed), we'll use this signature:
-- Example implementation of genRandom (replace with your professor's version) genRandom :: Int -> Int -> ([Int], Int) genRandom len seed = let randSeq = take len $ iterate (\x -> (x * 1103515245 + 12345) `mod` 2^31) seed newSeed = last randSeq in (map (`mod` 10) randSeq, newSeed)
Now, a helper to add c random lists to a single sublist while updating the seed:
addRandomToSublist :: [Int] -> Int -> Int -> ([Int], Int) addRandomToSublist sublist 0 seed = (sublist, seed) -- Base case: no more random lists to add addRandomToSublist sublist c seed = let (randList, newSeed) = genRandom 2 seed (updatedSublist, finalSeed) = addRandomToSublist (sublist ++ randList) (c-1) newSeed in (updatedSublist, finalSeed)
Then, apply this helper to all sublists in the base list, chaining seeds between sublists:
addRandomToAll :: [[Int]] -> Int -> Int -> ([[Int]], Int) addRandomToAll [] _ seed = ([], seed) addRandomToAll (x:xs) c seed = let (updatedX, newSeed1) = addRandomToSublist x c seed (updatedXs, newSeed2) = addRandomToAll xs c newSeed1 in (updatedX : updatedXs, newSeed2)
Finally, the main function that combines all parts:
genLists :: Int -> Int -> Int -> [[Int]] genLists n c seed = let baseLists = genBaseLists n (result, _) = addRandomToAll baseLists c seed in result
If genRandom Doesn't Return a New Seed
If your professor's genRandom only returns a list (no new seed), you can manually increment the seed between calls (note: this is less random but works for assignment purposes):
addRandomToSublist :: [Int] -> Int -> Int -> [Int] addRandomToSublist sublist 0 _ = sublist addRandomToSublist sublist c seed = let randList = genRandom 2 seed in addRandomToSublist (sublist ++ randList) (c-1) (seed + 1) addRandomToAll :: [[Int]] -> Int -> Int -> [[Int]] addRandomToAll [] _ _ = [] addRandomToAll (x:xs) c seed = let updatedX = addRandomToSublist x c seed in updatedX : addRandomToAll xs c (seed + c) genLists :: Int -> Int -> Int -> [[Int]] genLists n c seed = addRandomToAll (genBaseLists n) c seed
Example Usage
Calling genLists 2 2 42 (2 sublists, 2 random lists per sublist, seed 42) might return:
[[0,5,7,4,8], [0,3,1,6,2]]
Each sublist starts with the fixed element 0, followed by two 2-element random lists flattened into the sublist.
内容的提问来源于stack exchange,提问作者user12174294

