Alexey Orlov
Alexey Orlov

Reputation: 2804

Haskell: recursive mapping difficulty

I'm working on recursive tree walking. I want something like the following (Haskell pseudocode):

walkTree step root = do
  dsc <- getDescendants root
  for d in dsc
    nextStep = increase step d
    walkTree nextStep d

No iteration for me, of course. I can't figure out the mapping solution, though. How can I manage the step thing?

UPD:

Let's try the mapping:

walkTree step root = do
  dsc <- getDescendants root
  nextStep = increase step d
  mapM (walkTree nextStep) dsc

Two problems: nextStep is frozen for the mapping; also cannot get d (which is secondary)

Upvotes: 1

Views: 66

Answers (1)

Alexey Orlov
Alexey Orlov

Reputation: 2804

Final solution, courtesy of user2407038 (see comments to the question). This is a snippet of running code (is it DFS?):

-- | Walks the source tree, recreates source tree at destination.
traverseTreeDst :: Settings -> FilePath -> Int -> Int -> Counter -> FilePath -> FilePath -> IO ()
traverseTreeDst args dstRoot total totw counter dstStep srcDir = do
  (dirs, files)        <- listDir args srcDir

  let traverse dir = do
        let step = dstStep </> basename dir
        mkdir (dstRoot </> step)
        traverseTreeDst   args dstRoot total totw counter step dir

  mapM_ traverse                                                   dirs
  mapM_ (copyFile         args dstRoot total totw counter dstStep) files

You probably see why I resorted to pseudocode. Lots of unnecessary detail, yet this is it. The thing copies audio files, mangles their names, and sets tags. Nothing much.

I wouldn't mind discussing the relevant terminology. How do you describe this kind of problem and this kind of solution?

Upvotes: 1

Related Questions