I've come to the realization that when I have nested data structures, I've been manually writing code to delve into them. Like this:
--one level
Prelude> map (*2) [1,2,3]
[2,4,6]
--nested two levels
Prelude> let t2 = map $ map (*2)
Prelude> t2 [[1,2,3],[4,5,6]]
[[2,4,6],[8,10,12]]
--nested three levels
Prelude> let t3 = map $ map $ map (*2)
Prelude> t3 [[ [1,2,3],[4,5,6] ],[ [1,2,3],[4,5,6] ]]
[[[2,4,6],[8,10,12]],[[2,4,6],[8,10,12]]]
so it occurs to me that I should be able to automatically construct a function for delving into my nested data structures using a higher order function:
Prelude> let t f n = (iterate map f) !! n
<interactive>:35:22:
Occurs check: cannot construct the infinite type: b0 = [b0]
Expected type: (a0 -> b0) -> a0 -> b0
Actual type: (a0 -> b0) -> [a0] -> [b0]
In the first argument of `iterate', namely `map'
In the first argument of `(!!)', namely `(iterate map f)'
Its strikes me that
- I understand its finding a list where it expected...something else
- I don't know how to fix this - should I write code to do repeated application even though thats what I thought iterate was for?
- This seems similar to the concept of "lifting" - but I don't know how to apply that intuition.