하스켈로 개미노트 드릴게요. 2.

저번의 후속.
이번부터 Aray 해금.순환에도 사용할 수 있어요.개미책'2-1 Lake Counting'을 준다.
오늘의 표절품은 여기.입니다.
다음은 코드입니다.
-- Lake Counting (POJ No.2386)
-- https://lvs7k.github.io/posts/2018/11/pccb-easy-1/
import Control.Monad
import Control.Monad.ST
import Data.Array.ST
import Data.Array.Unboxed (UArray)
import Data.Array.IArray
import Data.Char
import Data.STRef
import qualified Data.Set as S
import Data.Typeable

solve :: Int -> Int -> [String] -> Int
solve n m xss = runST $ do
    as <- newListArray ((1, 1), (n, m)) (concat xss) :: ST s (STUArray s (Int, Int) Char)
    refc <- newSTRef 0
    forM_ [1 .. n] $ \i -> do
        forM_ [1 .. m] $ \j -> do
            c <- readArray as (i, j)
            when (c == 'W') $ do
                dfs as (i, j)
                modifySTRef' refc (+ 1)
    readSTRef refc
    where
        dfs as (i, j) = do
            c <- readArray as (i, j)
            case c of
                '.' -> return ()
                _   -> do
                    writeArray as (i, j) '.'
                    mapM_ (dfs as) next
            where
                next = do
                    s <- [i - 1, i, i + 1]
                    t <- [j - 1, j, j + 1]
                    guard (inRange ((1, 1), (n, m)) (s, t))
                    return (s, t)

rInt :: String -> Int
rInt str = read str :: Int

main :: IO ()
main = do 
    n <- rInt <$> getLine
    m <- rInt <$> getLine
    inp <- getContents
    let garden = lines inp
    -- print (typeOf garden)
    print garden
    print $ solve n m garden
이것은main입니다.hs로 만들다txt 미리 만들기
10
12
W........WW.
.WWW.....WWW
....WW...WW.
.........WW.
.........W..
..W......W..
.W.W.....WW.
W.W.W.....W.
.W.W......W.
..W.......W.
집행.
stack runghc main.hs < data.txt
그런데 그렇게 간단하지 않아요.착오가 되다.
제가 알아볼게요
main.hs:27:9: error:
    • Non type-variable argument in the constraint: MArray a Char m
      (Use FlexibleContexts to permit this)
    • When checking the inferred type
        dfs :: forall (m :: * -> *) (a :: * -> * -> *).
               MArray a Char m =>
               a (Int, Int) Char -> (Int, Int) -> m ()
      In an equation for ‘solve’:
          solve n m xss
            = runST
                $ do as <- newListArray ((1, 1), (n, m)) (concat xss) ::
                             ST s (STUArray s (Int, Int) Char)
                     refc <- newSTRef 0
                     forM_ [1 .. n] $ \ i -> ...
                     ....
            where
                dfs as (i, j)
                  = do c <- readArray as ...
                       ....
                  where
                      next = ...
   |
27 |         dfs as (i, j) = do
   |         ^^^^^^^^^^^^^^^^^^...
.
solve :: Int -> Int -> [String] -> Int
solve n m xss = runST $ do
    as <- newListArray ((1, 1), (n, m)) (concat xss) :: ST s (STUArray s (Int, Int) Char)
    refc <- newSTRef 0
    forM_ [1 .. n] $ \i -> do
        forM_ [1 .. m] $ \j -> do
            c <- readArray as (i, j)
            when (c == 'W') $ do
                dfs as (i, j)
                modifySTRef' refc (+ 1)
    readSTRef refc
  where
    dfs as (i, j) = do
        c <- readArray as (i, j)
        case c of
            -- '.' -> return ()
            _   -> do
                -- writeArray as (i, j) '.'
                mapM_ (dfs as) next
      where
        next = do
            s <- [i - 1, i, i + 1]
            t <- [j - 1, j, j + 1]
            guard (inRange ((1, 1), (n, m)) (s, t))
            return (s, t)
주석이 끊긴 곳을 사용하면 구축 오류가 발생합니다.사이즈가 안 맞아요.
"."안 될 것 같은데, 왜 그런가.

좋은 웹페이지 즐겨찾기