Comment detail

BFコンパイラー (Nested Flatten)

This comment is reply for 3986 nobsun: 仮想機械(機械状態とインストラクションセ...(BFコンパイラー). Go to thread root.

高階関数を使ってみたい年頃なので高階関数使った版findpc を書いてみました。

findpc はループがネストしているとき '[' が来るごとに何度も呼ばれるのが
ちょっともったいない気もします。

それと hClose oh しないとバッファが flush されませんでした。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
-- import Maybe
-- import Control.Monad

-- -- where pc = findpc [] cs
-- where pc = findpc cs

findpc cs = fromMaybe (error "syntax error")
  $ msum $ snd $ mapAccumL f 0 cs  where
    f 0 (p,']') = (0, Just (p+1))
    f s (_,c) = (s+d, Nothing) where
      d = case c of; ']' -> (-1); '[' -> 1; _ -> 0

Index

Feed

Other

Link

Pathtraq

loading...