[Haskell-cafe] foreach

Bulat Ziganshin bulat.ziganshin at gmail.com
Wed Sep 13 07:24:39 EDT 2006


Hello Udo,

Wednesday, September 13, 2006, 12:53:38 PM, you wrote:

>>  main = do
>>    args <- getArgs
>>    flip mapM_ args $ \arg ->
>>      flip mapM_ [1..3] $ \n ->
>>        putStrLn $ show n ++ ") " ++ arg

> Or even:

> main = do
>     args <- getArgs
>     putStr $ unlines [ show n ++ ") " ++ arg
>                      | arg <- args, n <- [1..3] ]

> I'm really at a loss trying to understand why some people seem to like
> the imperative style.  In fact, most  of the time, the strings in the
> code above are better replaced by Doc from Text.PrettyPrint.

because REAL code is somewhat larger than examples. try to rewrite the
following:

  directory_blocks  <-  (`mapM` splitBy (opt_group_dir command) files_to_archive)
    ( \filesInOneDirectory -> do
      datablocks  <-  (`mapM` splitToSolidBlocks filesInOneDirectory)
        ( \filesInOneDataBlock -> do     
          let compressor = map (freearcLimitDictionary$ clipToMaxInt totalBytes)
                               (data_compressor filesInOneDataBlock)
              totalBytes = sum$ map (fiSize.cfFileInfo) filesInOneDataBlock
              copy_solid_block = isWholeSolidBlock filesInOneDataBlock
          writeBlock pipe DATA_BLOCK compressor copy_solid_block $ do
            dir <- if copy_solid_block then do
                     sendP pipe (CopySolidBlock filesInOneDataBlock)
                     return$ map fileWithCRC filesInOneDataBlock   
                   else if (compressor==[aFAKE_COMPRESSION]) then do
                     sendP pipe (FakeFiles filesInOneDataBlock)
                     return$ map (FileWithCRC 0 . cfFileInfo) filesInOneDataBlock
                   else do
                     mapMaybeM (read_file command bufOps decompress_pipe) filesInOneDataBlock
            processDir dir   
            return dir
        )
      blocks_info  <-  replicateM (length datablocks) (getP backdoor)
      arcpos <- archiveGetPos archive
      
      writeControlBlock DIR_BLOCK dir_compressor $ do
        archiveWriteDir blocks_info arcpos receiveBuf sendBuf
    )




-- 
Best regards,
 Bulat                            mailto:Bulat.Ziganshin at gmail.com



More information about the Haskell-Cafe mailing list