Drkcore

19 01 2010 Haskell Tweet

(続) 行番号付きでソースを出力(Haskellで)

前回の行番号付きでファイルの出力に、さらに5行出力するごとにファイル名行番号を反転表示した後、一時停止して、リターンを押したら出力を再開するプログラムをかけという問題が丸投げされていたのでやってみた。

import System
import System.Console.Readline (readline)

showNext xs file = showNext' 1 xs file

showNext' n xs f | next == [] = do mapM_ putStrLn $ zipWith (\n x -> show n ++ ": " ++  x) [n..]  this
                 | otherwise  = do mapM_ putStrLn $ zipWith (\n x -> show n ++ ": " ++  x) [n..]  this
                                   maybeLine <- readline $ "\ESC[7m" ++ f ++ "(" ++ show (n+4) ++"):\ESC[m"
                                   case maybeLine of
                                     Nothing   -> return ()
                                     Just ""   -> showNext' (n+5) next f
                 where
                   (this,next) = splitAt 5 xs

main = do
  file:_ <- getArgs
  content <- readFile file
  showNext (lines content) file

反転表示を戸惑ったのだけど、教えてもらって解決。

showNext'が冗長なので、もう少し綺麗に書きたい。Stateモナドとか使うといいのかなぁ。

About

  • もう5年目(wishlistありマス♡)
  • 最近はPythonとDeepLearning
  • 日本酒自粛中
  • ドラムンベースからミニマルまで
  • ポケモンGOゆるめ

Tag

Python Deep Learning javascript chemoinformatics Emacs sake and more...

Ad

© kzfm 2003-2021