Haskell Recursion: interleave function in Programming in Haskell ch.11 -


i cannot figure out how recursive mechanism following 'interleave' function in programming in haskell ch.11 works.

-- definition interleave :: -> [a] -> [[a]] interleave [] = [[a]] interleave x (y:ys) = (x:y:ys):map (y:) (interleave x ys)  -- example > interleave 3 [4,5,6,7] [[3,4,5,6,7],[4,3,5,6,7],[4,5,3,6,7],[4,5,6,3,7],[4,5,6,7,3]] 

how work under hood?

i stuck @ reasoning this:

interleave 3 [4,5,6,7] = [3,4,5,6,7]:map (4:) (interleave 3 [5,6,7]) = [3,4,5,6,7]:[4,3,5,6,7]:map (5:) (interleave 3 [6,7]) = [3,4,5,6,7]:[4,3,5,6,7]:[5,3,6,7]:map (6:) (interleave 3 [7]) = [3,4,5,6,7]:[4,3,5,6,7]:[5,3,6,7]:[6,3,7]:map (7:) (interleave 3 []) = [3,4,5,6,7]:[4,3,5,6,7]:[5,3,6,7]:[6,3,7]:[7,3]:[[3]]  = [[3,4,5,6,7],[4,3,5,6,7],[5,3,6,7],[6,3,7],[7,3],[3]] ??? /= [[3,4,5,6,7],[4,3,5,6,7],[4,5,3,6,7],[4,5,6,3,7],[4,5,6,7,3]] 

please enlighten me.. many in advance.

between lines 2 , 3 miss something:

[3,4,5,6,7]:map (4:) (interleave 3 [5,6,7]) = [3,4,5,6,7]:map (4:) ([3,5,6,7]:map (5:) (interleave 3 [6,7])) = [3,4,5,6,7]:[4,3,5,6,7]:map (4:) (map (5:) (interleave 3 [6,7])) 

so @ end have bunch of map (x:) , all lists contain elements of original list


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -