Effective Haskell: Chapter 10 - directorySummaryWithMetrics (B9 - PDF version)

@rebeccaskinner

On p.386, we have the final version of directorySummaryWithMetrics.

When running the code, we see the histogram results with empty characters.
It looks like this


Histogram Data:
 : 9091
    : 59
 : 23
 : 1
    : 2
  : 59
 : 12900
    : 643147
 : 20
  : 22
 : 563
  : 6
     : 666771
    !: 1476
    ": 137317
    #: 7367

    ...

The non-printable characters do not display. You can see the empty fields above ASCII value 20 (a total of 666771 spaces here).

Here are the last few lines of the function:

   histogram <- readIORef histogramRef
   putStrLn "Histogram Data:"
   for_ (Map.toList histogram) $ \(letter, count) ->
       putStrLn $ printf "  %c: %d" letter count

   displayMetrics metrics

I suggest to add a check using isControl.

import qualified Data.Char as Char (isControl)
   
   ...

   histogram <- readIORef histogramRef
   putStrLn "\n\nHistogram Data:"                    -- Added "\n\n"
   for_ (Map.toList histogram) $ \(letter, count) ->
      if Char.isControl letter then                   -- Added if .. then ...
           putStrLn $ printf "  %d: %d" letter count
      else
           putStrLn $ printf "  %c: %d" letter count

   displayMetrics metrics


Andre