15 May 20:03 2013

## recursion patterns?

7stud <7stud <at> excite.com>

2013-05-15 18:03:49 GMT

2013-05-15 18:03:49 GMT

Well, my question does not meet the standards for a question at stackoverflow: (I am a haskell beginner) This example is from LYAH: import System.Random finiteRandoms :: (RandomGen g, Random a, Num n) => n -> g -> ([a], g) finiteRandoms 0 gen = ([], gen) finiteRandoms n gen = let (value, newGen) = random gen (restOfList, finalGen) = finiteRandoms (n-1) newGen in (value:restOfList, finalGen) Looking at that function, I find it impossible to understand how that recursion works. I have to pull out a pencil and paper to figure it out. If I was given the task of writing that function, I would write it like this: import System.Random finiteRands :: (RandomGen g, Random a) => Int -> g -> [a] finiteRands n gen = finiteRands' n gen [] finiteRands' :: (RandomGen g, Random a) => Int -> g -> [a] -> [a] finiteRands' 0 _ acc = acc finiteRands' n gen acc = let (rand_num, new_gen) = random gen in finiteRands' (n-1) new_gen (rand_num:acc) To me, it makes the function(s) simplistic to understand. Is the first solution a known 'design pattern' in(Continue reading)