Drkcore

16 01 2010 Haskell Tweet

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

mixiの課題丸投げから

$ runhaskell mixi100116.hs mixi100116.hs
1: import System
2: 
3: main = do
4:   file:_ <- getArgs
5:   content <- readFile file
6:   mapM_ putStrLn $ withNum 1 $ lines content
7:   where 
8:     withNum n [] = []
9:     withNum n (x:xs)  = ((show n) ++ ": " ++ x) : (withNum (n+1) xs)

追記

zipWithがあるじゃないか。忘れてた。

1: import System
2: 
3: main = do
4:   file:_ <- getArgs
5:   content <- readFile file
6:   mapM_ putStrLn $ zipWith (\n x -> show n ++ ": " ++  x) [1..]  (lines content)

数字の連番くっつけたいときはそういうリストを用意してまぜ合わせる。

About

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

Tag

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

Ad

© kzfm 2003-2021