Hi everyone,
I had an idea and wanted to implement it
obviously we know map and what it does on a list
but say you have a list of lists, and you wanna map the elements in the deepest sublist
you’d put something like map (map f) xs
but what if you want to generalize this for a an n-nested list?
what if you want to pass any nested list to some function and it always maps the individual elements in
the deepest sublist
So I kinda wanted to make this function
nest :: [[[Integer]]]
nest = [[[1, 1], [2, 2], [3, 3], [4, 4]], [[5, 5], [6, 6], [7, 7], [8, 8]], [[9, 9], [10, 10], [11, 11], [12, 12]]]
nestedMap :: [a] -> Int -> (a -> b) -> [a]
nestedMap [] _ _ = []
nestedMap xs n f
| n > 0 = map nestedMap xs (n - 1) f
| n == 0 = (map f) xs
nest is a test variabel in this code, and nestedMap is supposed to take care of the work
n is basically the degree that you pass along. n eventually hits zero for any nested list
so I basically wanted to create an expression of maps upon maps upon maps and then eventually
applied with f on xs. However this code gives bugs. type wrong this that. probably some parentheses wrong too if I compare it to the actual expression of map (map f) xs for 2 dimensional lists
Is there a way to create something like this?
I’m curious to find out
*** side note: f maps a’s to b’s, yet the final typing is still [a], but yeah I can’t just put [b] right
because it should work for any nested list. Like I don’t wanna just do something static like
[[[a]]] …-> …[[[b]]]