乱入3

http://d.hatena.ne.jp/w_o/20050309#p1

インデントがブロックといえばPythonHaskellですけれども私はPythonを知りませんのでHaskellで。

module Main(main) where

import System
import System.IO
import System.Directory
import System.Time
import Data.Char

data CompareMode = Newer | Older deriving Eq

readMode contents = case contents of
  'n':xs -> (Newer, xs)
  'o':xs -> (Older, xs)
  _ -> error "n/o"

endOfLine contents = case contents of
  x:xs -> (x == '\r') || (x == '\n')
  _ -> True

readInteger contents last = readIntegerInternal contents 0
  where
    readIntegerInternal rs value = case rs of
      x:xs -> if x == last 
        then (value, xs)
        else if isDigit x
          then let result = value * 10 + ((ord x) - (ord '0')) in
            if endOfLine xs 
              then (result, xs)
              else readIntegerInternal xs result
          else error "parsing"
      _ -> (value, "")

skipAll contents = case contents of
  x:xs -> skipAll xs
  _ -> return ()

main = do 
  arguments <- getArgs
  case arguments of
    [fileName] -> do
      fileClockTime <- getModificationTime fileName
      fileTime <- toCalendarTime fileClockTime
      handle <- openFile fileName ReadMode
      contents1 <- hGetContents handle
      let (mode, contents2) = readMode contents1 in do
      case contents2 of
        ',':contents3 -> do
          let (hour, contents4) = readInteger contents3 ':' in do
          let (min, contents5) = readInteger contents4 '\0' in do
          skipAll contents5
          hClose handle
          if (hour >= 24) || (min >= 60)
            then putStrLn "error:range"
            else if (mode == Newer) == ((ctHour fileTime) * 60 + (ctMin fileTime) > hour * 60 + min)
              then putStrLn "ok"
              else putStrLn "ng"
        _ -> error "\",\""
    _ -> putStrLn "usage:"

ファイル入力初使用です…が…skipAllが無いとおかしくなるのは何故でしょう…。
本論は無視とか言っておいてなんですが、私はインデントが一気に二段以上戻る*1のが嫌いというほどでもありませんがなんとなく避けたいですので、Adaが好きです。

あー…えーと…別に何がしたいというわけでもなく…最近プログラムから遠ざかってましたのでリハビリといいますか…。これなら書いてもいいと思わせるようなお題をぶらさげたのが不運だったとでも思っていただければ…(意味不明)。

*1:CやPascalのぶらさがりifでも発生