17/05/11 17:14:36.14 +i4P6kRh.net
はいはい・・・。
んじゃ、検索で見つけた位置も追加で表示するように拡張。
import System.Environment
search w = search' (False,0,[],(1,0)) w
where
search' (b,n,ps,_) _ [] = (b,n,reverse ps)
search' (b,n,ps,(y,_)) w (c:cs) | c == '\n' = search' (b,n,ps,(y + 1,0)) w cs
search' (_,n,ps,(y,x)) w (c:cs) | take (length w) (c:cs) == w = search' (True,n + 1,(y,x + 1):ps,(y,x + 1)) w cs
search' (b,n,ps,(y,x)) w (_:cs) = search' (b,n,ps,(y, x + 1)) w cs
search2 w = search' (False,0,[],(1,0)) w
where
search' (b,n,ps,_) _ [] = (b,n,reverse ps)
search' (b,n,ps,(y,_)) w (c:cs) | c == '\n' = search' (b,n,ps,(y + 1,0)) w cs
search' (_,n,ps,(y, x)) w cts | take (length w) cts == w = search' (True,n + 1,(y, x + 1):ps,(y, x + 1)) w $ drop (length w) cts
search' (b,n,ps,(y,x)) w (_:cs) = search' (b,n,ps,(y, x + 1)) w cs
-- main = do 7777
-- (file:word:_) <- getArgs
-- content <- readFile file
-- print $ search word content
main = getArgs >>= \(file:word:_) -> readFile file >>= print.search2 word