title: how to map functions to multi-level lists in Haskell
body: <p>This is my first post here, and I have to introduce myself by saying that I am a Haskell beginner. I love the promise of pure functional programming, but after 20 years of imperative programming (mostly Java), Haskell does not come naturally to me.</p>
  
  <p>So here's my first question.
  I am trying to map a function to a multi-level list.
  This code works, but the depth level of the list where the mapping occurs is preset at 3:</p>
  
  <pre><code>(map . map . map) (+1) [[[1,4],[2,3]],[[5]]]
  </code></pre>
  
  <p>I need something more general, where I specify the level where the mapping happens (depth parameter in the line below), like so:</p>
  
  <pre><code>let depth = 3 :: Int
  (foldl (.) id (replicate depth map)) (+1) [[[1,4],[2,3]],[[5]]]
  </code></pre>
  
  <p>Unfortunately, this does not work, I get this error:</p>
  
  <pre><code>Occurs check: cannot construct the infinite type: t0 = [t0]
  Expected type: ([[[t0]]] -&gt; [[[t0]]]) -&gt; [[[t0]]] -&gt; [[[t0]]]
    Actual type: ([[[t0]]] -&gt; [[[t0]]]) -&gt; [[[[t0]]]] -&gt; [[[[t0]]]]
  In the second argument of `replicate', namely `map'
  In the third argument of `foldl', namely `(replicate depth map)'
  In the expression:
    (foldl (.) id (replicate depth map))
      (+ 1) [[[1, 4], [2, 3]], [[5]]]
  </code></pre>
  
  <p>However, this code works:</p>
  
  <pre><code>foldl (.) id (replicate 3 negate) $ 7
  </code></pre>
  
  <p>So again, the questions is: how do I map a function onto a multi-level list, where the depth of the list is known in advance. I would like to do something like:</p>
  
  <pre><code>(foldl (.) id (replicate 3 map)) (+1) [[[1,4],[2,3]],[[5]]]
  </code></pre>
  
  <p>and get this result back:</p>
  
  <pre><code>[[[2,5],[3,4]],[[6]]]
  </code></pre>
  
  <p>Thank you in advance.</p>