{"id":340120,"date":"2022-10-23T15:00:27","date_gmt":"2022-10-23T15:00:27","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=340120"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=340120","title":{"rendered":"<span>\u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f State \u0438 StateT<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041c\u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0430\u0441\u044c \u0444\u0440\u0430\u0437\u0430: &#171;\u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0441 Haskell \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u043e\u043d\u0430\u0434\u0430\u0445&#187;. \u041c\u043e\u043d\u0430\u0434\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u044b \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0430 \u0441\u0430\u043c\u0430\u044f \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u0430\u044f, \u043b\u0438\u0447\u043d\u043e \u0434\u043b\u044f \u043c\u0435\u043d\u044f, \u0431\u044b\u043b\u0430 \u043c\u043e\u043d\u0430\u0434\u0430 State.<\/p>\n<p>\u041d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0441\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u043d\u0430\u0434\u044b State \u0438 \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 StateT.<\/p>\n<h2>\u0418\u0434\u0435\u044f<\/h2>\n<ul>\n<li>\n<p>\u0415\u0441\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u0441\u0442\u044b\u0435 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u043e\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c &#8216;O&#8217;<\/p>\n<\/li>\n<li>\n<p>\u0421\u0438\u043c\u0432\u043e\u043b\u043e\u043c &#8216;X&#8217; \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d &#171;\u0433\u0435\u0440\u043e\u0439&#187;, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0438\u0433\u0440\u043e\u0432\u043e\u043c\u0443 \u043f\u043e\u043b\u044e \u0432\u0432\u0435\u0440\u0445, \u0432\u043d\u0438\u0437, \u0432\u043b\u0435\u0432\u043e, \u0432\u043f\u0440\u0430\u0432\u043e.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0418\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f. \u041e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u043c. \u0412 \u043d\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0433\u0435\u0440\u043e\u044f:<\/p>\n<pre><code class=\"haskell\">data GameField = GameField Int (Int, Int)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u043a \u0441\u0442\u0440\u043e\u043a\u0435, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u043c Data.Array \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"haskell\">listArray :: Ix i => (i, i) -> [e] -> Array i e (\/\/)      :: Ix i => Array i e -> [(i, e)] -> Array i e elems     :: Array i e -> [e]<\/code><\/pre>\n<p>listArray \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0438\u0436\u043d\u044e\u044e \u0438 \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432. \u0421\u043f\u0438\u0441\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u043b\u0438\u0448\u044c \u0431\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0442\u043e\u0447\u043d\u043e \u0445\u0432\u0430\u0442\u0438\u0442, repeat \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0418 \u0434\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u0447\u0438\u0441\u0435\u043b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0430\u0440\u044b, \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u0433\u0440\u0430\u0442\u044c \u0440\u043e\u043b\u044c \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442.<\/p>\n<pre><code class=\"haskell\">listArray ((1,1), (3,3)) (repeat 'O') array ((1,1),(3,3)) [((1,1),'O'),((1,2),'O'),((1,3),'O')                     ,((2,1),'O'),((2,2),'O'),((2,3),'O')                     ,((3,1),'O'),((3,2),'O'),((3,3),'O')]<\/code><\/pre>\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 (\/\/) \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440 (\u0438\u043d\u0434\u0435\u043a\u0441, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435). \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e (\/\/) \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0433\u0435\u0440\u043e\u044f \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b<\/p>\n<pre><code class=\"haskell\">arr \/\/ [((2,2), 'X')] array ((1,1),(3,3)) [((1,1),'O'),((1,2),'O'),((1,3),'O')                     ,((2,1),'O'),((2,2),'X'),((2,3),'O')                     ,((3,1),'O'),((3,2),'O'),((3,3),'O')]<\/code><\/pre>\n<p>elems \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435). \u0414\u0430\u043b\u0435\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u043d\u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043f\u0438\u0441\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0435\u0440\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 unlines \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u0421\u0435\u0439\u0447\u0430\u0441 \u0444\u0430\u0439\u043b GameField.hs \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">module GameField where  import           Data.Array  data GameField = GameField Int (Int, Int)  instance Show GameField where   show (GameField s h) =     unlines $ splitString $ elems $ (\/\/ [(h, 'X')]) $ listArray       ((1, 1), (s, s))       (repeat 'O')    where     splitString :: String -> [String]     splitString \"\"  = []     splitString str = let (l, rest) = splitAt s str in l : splitString rest<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f<\/p>\n<pre><code class=\"haskell\">move :: Char -> GameField -> GameField move 'W' (GameField s (yH, xH)) = GameField s ((yH - 1) `max` 1, xH) move 'A' (GameField s (yH, xH)) = GameField s (yH, (xH - 1) `max` 1) move 'S' (GameField s (yH, xH)) = GameField s ((yH + 1) `min` s, xH) move 'D' (GameField s (yH, xH)) = GameField s (yH, (xH + 1) `min` s) move _   gf                     = gf<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0438\u0441\u0438 \u0442\u0438\u043f\u0430 <\/p>\n<pre><code class=\"haskell\">(yH - 1) max 1  (yH + 1) min s<\/code><\/pre>\n<p>\u0431\u0443\u0434\u0443\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0433\u0435\u0440\u043e\u0439 \u043d\u0435 \u0432\u044b\u0448\u0435\u043b \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043f\u043e\u043b\u044f<\/p>\n<h2>\u0418\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435, \u043a\u0430\u043a \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/h2>\n<p>\u041c\u043e\u043d\u0430\u0434\u044b \u0432 Haskell \u044d\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u043e\u043c. \u041f\u043e\u0431\u043e\u0447\u043d\u044b\u0439 \u044d\u0444\u0444\u0435\u043a\u0442 \u043c\u043e\u043d\u0430\u0434\u044b State &#8212; \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"haskell\">State s a, \u0433\u0434\u0435 s - \u043a\u0430\u043a\u043e\u0435-\u043b\u0438\u0431\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435,                 a - \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u043e\u0435 \u043a\u0430\u043a\u0438\u043c \u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<pre><code class=\"haskell\">heroMove :: Char -> State GameField () heroMove ch = modify (move ch)<\/code><\/pre>\n<p>\u043e\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0438\u043c\u0432\u043e\u043b, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u043e\u043d\u0430\u0434\u0443 State, \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 GameField (\u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435), \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (). \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0430: \u0444\u0443\u043d\u043a\u0446\u0438\u044f modify \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e (s -> s), (GameField -> GameField) \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<pre><code class=\"haskell\">pathMove :: String -> State GameField () pathMove = mapM_ heroMove<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e \u0441\u0443\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0443\u0442\u0435\u043c, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 \u043d\u0430\u0448 \u0433\u0435\u0440\u043e\u0439. \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437<\/p>\n<pre><code class=\"haskell\">mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m () -- \u0430 \u0435\u0441\u043b\u0438 \u0431\u0435\u0437 \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u0438\u0437\u043c\u0430 mapM_ :: (Char -> State GameField ()) -> String -> State GameField ()<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u0435\u0434\u0435\u0442 \u043f\u0443\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u0444\u0430\u0439\u043b\u0430 GameFieldState.hs:<\/p>\n<pre><code class=\"haskell\">module GameFieldState where  import           Control.Monad.State import           GameField  heroMove :: Char -> State GameField () heroMove ch = modify (move ch)  pathMove :: String -> State GameField () pathMove = mapM_ heroMove  main :: IO () main = do   let gf = GameField 3 (2, 2) -- \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f   print gf                    -- \u0432\u044b\u0432\u043e\u0434 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f   path &lt;- getLine             -- \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u043e\u0434\u0430   print $ execState (pathMove path) gf -- \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0438 \u0432\u044b\u0432\u043e\u0434 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f<\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432 ghci:<\/p>\n<pre><code class=\"haskell\">:l GameFieldState main OOO OXO OOO  WA XOO OOO OOO<\/code><\/pre>\n<p>\u0412\u0432\u0435\u0434\u044f \u0441\u0442\u0440\u043e\u043a\u0443 WA, \u043c\u044b &#171;\u0437\u0430\u0433\u043d\u0430\u043b\u0438&#187; \u0433\u0435\u0440\u043e\u044f \u0432 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u0443\u0433\u043e\u043b.<\/p>\n<h2>StateT &#8212; \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438<\/h2>\n<p>\u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0432\u0435\u0441\u044c \u043f\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443, \u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u043c \u0448\u0430\u0433\u043e\u043c \u0433\u0435\u0440\u043e\u044f \u0438 \u0432\u0438\u0434\u0435\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430\u043c \u043c\u043e\u043d\u0430\u0434 \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e.<\/p>\n<p>\u0422\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u0433\u0435\u0440\u043e\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u0436\u0435 \u0442\u0430\u043a\u043e\u0439:<\/p>\n<pre><code class=\"haskell\">heroMove :: StateT GameField IO ()<\/code><\/pre>\n<p>\u0423\u0436\u0435 \u043d\u0435\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u0430(Char) \u0442\u043e\u043b\u044c\u043a\u043e<\/p>\n<pre><code class=\"haskell\">StateT s m a, s - \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435               a - \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435               m - \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u043c\u043e\u043d\u0430\u0434\u0430<\/code><\/pre>\n<p>IO \u0431\u0443\u0434\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0439 \u043c\u043e\u043d\u0430\u0434\u043e\u0439. \u0424\u0430\u0439\u043b GameFieldStateT.hs \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a<\/p>\n<pre><code class=\"haskell\">module GameFieldStateT where  import           Control.Monad.Trans import           Control.Monad.Trans.State import           GameField  heroMove :: StateT GameField IO () heroMove = do   gf &lt;- get           -- \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f   lift $ print gf     -- \u0432\u044b\u0432\u043e\u0434, lift \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 IO    ch &lt;- lift getChar  -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f   lift $ putStrLn \"\"  -- \u043d\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435   modify $ move ch    -- \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u0430\u044f modify \u0438 move ch   heroMove            -- \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, \u043d\u0435\u0442 \u043f\u043e\u043a\u043e\u044f \u0433\u0435\u0440\u043e\u044e  main :: IO () main = evalStateT heroMove (GameField 4 (2, 2)) -- \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 StateT<\/code><\/pre>\n<p>\u0418\u0441\u043f\u044b\u0442\u0430\u0435\u043c \u0432 ghci:<\/p>\n<pre><code class=\"haskell\">:l GameFieldStateT main OOOO OXOO OOOO OOOO  d OOOO OXOO OOOO OOOO  D OOOO OOXO OOOO OOOO  S OOOO OOOO OOXO OOOO<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043b\u0438\u0447\u043d\u043e \u043c\u0435\u043d\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u0443\u0433\u0430\u043b\u0430 \u043c\u043e\u043d\u0430\u0434\u0430 State. \u0422\u043e\u0433\u0434\u0430 \u043c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u0441\u044f \u0431\u044b \u043f\u0440\u0438\u043c\u0435\u0440 \u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u0432\u0438\u0434\u0438\u0448\u044c, \u043a\u0430\u043a \u0447\u0442\u043e-\u0442\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0435 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/694944\/\"> https:\/\/habr.com\/ru\/post\/694944\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041c\u043d\u0435 \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u043b\u0430\u0441\u044c \u0444\u0440\u0430\u0437\u0430: &#171;\u0434\u043b\u044f \u043c\u043d\u043e\u0433\u0438\u0445 \u0437\u043d\u0430\u043a\u043e\u043c\u0441\u0442\u0432\u043e \u0441 Haskell \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u043c\u043e\u043d\u0430\u0434\u0430\u0445&#187;. \u041c\u043e\u043d\u0430\u0434\u044b \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u044b \u0434\u043b\u044f \u043f\u043e\u043d\u0438\u043c\u0430\u043d\u0438\u044f, \u0430 \u0441\u0430\u043c\u0430\u044f \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u0430\u044f, \u043b\u0438\u0447\u043d\u043e \u0434\u043b\u044f \u043c\u0435\u043d\u044f, \u0431\u044b\u043b\u0430 \u043c\u043e\u043d\u0430\u0434\u0430 State.<\/p>\n<p>\u041d\u0430 \u043f\u0440\u043e\u0441\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435, \u044f \u0445\u043e\u0447\u0443 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0432\u0441\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c \u043c\u043e\u043d\u0430\u0434\u044b State \u0438 \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0443\u044e \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430 StateT.<\/p>\n<h2>\u0418\u0434\u0435\u044f<\/h2>\n<ul>\n<li>\n<p>\u0415\u0441\u0442\u044c \u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/p>\n<\/li>\n<li>\n<p>\u041f\u0443\u0441\u0442\u044b\u0435 \u044f\u0447\u0435\u0439\u043a\u0438 \u043f\u043e\u043b\u044f \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d\u044b \u0441\u0438\u043c\u0432\u043e\u043b\u043e\u043c &#8216;O&#8217;<\/p>\n<\/li>\n<li>\n<p>\u0421\u0438\u043c\u0432\u043e\u043b\u043e\u043c &#8216;X&#8217; \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u043e\u0437\u043d\u0430\u0447\u0435\u043d &#171;\u0433\u0435\u0440\u043e\u0439&#187;, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043c\u043e\u0436\u0435\u0442 \u043f\u0435\u0440\u0435\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u043f\u043e \u0438\u0433\u0440\u043e\u0432\u043e\u043c\u0443 \u043f\u043e\u043b\u044e \u0432\u0432\u0435\u0440\u0445, \u0432\u043d\u0438\u0437, \u0432\u043b\u0435\u0432\u043e, \u0432\u043f\u0440\u0430\u0432\u043e.<\/p>\n<\/li>\n<\/ul>\n<h2>\u0418\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435<\/h2>\n<p>\u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f. \u041e\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043a\u0432\u0430\u0434\u0440\u0430\u0442\u043d\u044b\u043c. \u0412 \u043d\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0440\u0430\u0437\u043c\u0435\u0440\u0435 \u044d\u0442\u043e\u0433\u043e \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u0438 \u043f\u043e\u0437\u0438\u0446\u0438\u044f \u0433\u0435\u0440\u043e\u044f:<\/p>\n<pre><code class=\"haskell\">data GameField = GameField Int (Int, Int)<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u0438\u0432\u0435\u0434\u0435\u043d\u0438\u044f \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f \u043a \u0441\u0442\u0440\u043e\u043a\u0435, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u043c\u043e\u0434\u0443\u043b\u0435\u043c Data.Array \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0442\u0440\u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u0438:<\/p>\n<pre><code class=\"haskell\">listArray :: Ix i => (i, i) -> [e] -> Array i e (\/\/)      :: Ix i => Array i e -> [(i, e)] -> Array i e elems     :: Array i e -> [e]<\/code><\/pre>\n<p>listArray \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043d\u0438\u0436\u043d\u044e\u044e \u0438 \u0432\u0435\u0440\u0445\u043d\u044e\u044e \u0433\u0440\u0430\u043d\u0438\u0446\u044b \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430, \u0441\u043f\u0438\u0441\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432. \u0421\u043f\u0438\u0441\u043e\u043a \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043b\u044e\u0431\u043e\u0439 \u0434\u043b\u0438\u043d\u044b, \u043b\u0438\u0448\u044c \u0431\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u0441\u043f\u0438\u0441\u043a\u0430 \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0434\u043b\u044f \u043f\u043e\u0441\u0442\u0440\u043e\u0435\u043d\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430. \u0411\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0433\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0442\u043e\u0447\u043d\u043e \u0445\u0432\u0430\u0442\u0438\u0442, repeat \u043a\u0430\u043a \u0440\u0430\u0437 \u0438 \u0441\u043e\u0437\u0434\u0430\u0435\u0442 \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0441\u043f\u0438\u0441\u043e\u043a. \u0418 \u0434\u0430 \u0432\u043c\u0435\u0441\u0442\u043e \u0447\u0438\u0441\u0435\u043b \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u043f\u0430\u0440\u044b, \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u0438\u0433\u0440\u0430\u0442\u044c \u0440\u043e\u043b\u044c \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442.<\/p>\n<pre><code class=\"haskell\">listArray ((1,1), (3,3)) (repeat 'O') array ((1,1),(3,3)) [((1,1),'O'),((1,2),'O'),((1,3),'O')                     ,((2,1),'O'),((2,2),'O'),((2,3),'O')                     ,((3,1),'O'),((3,2),'O'),((3,3),'O')]<\/code><\/pre>\n<p>\u041e\u043f\u0435\u0440\u0430\u0442\u043e\u0440 (\/\/) \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432, \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440 (\u0438\u043d\u0434\u0435\u043a\u0441, \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435). \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e (\/\/) \u043c\u043e\u0436\u043d\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0433\u0435\u0440\u043e\u044f \u0432 \u0437\u0430\u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u043e\u043e\u0440\u0434\u0438\u043d\u0430\u0442\u044b<\/p>\n<pre><code class=\"haskell\">arr \/\/ [((2,2), 'X')] array ((1,1),(3,3)) [((1,1),'O'),((1,2),'O'),((1,3),'O')                     ,((2,1),'O'),((2,2),'X'),((2,3),'O')                     ,((3,1),'O'),((3,2),'O'),((3,3),'O')]<\/code><\/pre>\n<p>elems \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0441\u043f\u0438\u0441\u043e\u043a \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u0432 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435). \u0414\u0430\u043b\u0435\u0435 \u0441\u043f\u0438\u0441\u043e\u043a \u0440\u0430\u0437\u0434\u0435\u043b\u0438\u043c \u043d\u0430 \u0441\u043f\u0438\u0441\u043e\u043a \u0441\u043f\u0438\u0441\u043a\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043e\u0431\u0435\u0440\u0435\u0442\u0441\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u0435\u0439 unlines \u0432 \u0441\u0442\u0440\u043e\u043a\u0443 \u0432 \u0437\u0430\u0434\u0443\u043c\u0430\u043d\u043d\u043e\u043c \u0432\u0438\u0434\u0435. \u0421\u0435\u0439\u0447\u0430\u0441 \u0444\u0430\u0439\u043b GameField.hs \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<pre><code class=\"haskell\">module GameField where  import           Data.Array  data GameField = GameField Int (Int, Int)  instance Show GameField where   show (GameField s h) =     unlines $ splitString $ elems $ (\/\/ [(h, 'X')]) $ listArray       ((1, 1), (s, s))       (repeat 'O')    where     splitString :: String -> [String]     splitString \"\"  = []     splitString str = let (l, rest) = splitAt s str in l : splitString rest<\/code><\/pre>\n<p>\u041e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0440\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u0435\u0440\u0435\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f<\/p>\n<pre><code class=\"haskell\">move :: Char -> GameField -> GameField move 'W' (GameField s (yH, xH)) = GameField s ((yH - 1) `max` 1, xH) move 'A' (GameField s (yH, xH)) = GameField s (yH, (xH - 1) `max` 1) move 'S' (GameField s (yH, xH)) = GameField s ((yH + 1) `min` s, xH) move 'D' (GameField s (yH, xH)) = GameField s (yH, (xH + 1) `min` s) move _   gf                     = gf<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0438\u0441\u0438 \u0442\u0438\u043f\u0430 <\/p>\n<pre><code class=\"haskell\">(yH - 1) max 1  (yH + 1) min s<\/code><\/pre>\n<p>\u0431\u0443\u0434\u0443\u0442 \u043a\u043e\u043d\u0442\u0440\u043e\u043b\u0438\u0440\u043e\u0432\u0430\u0442\u044c, \u0447\u0442\u043e\u0431\u044b \u0433\u0435\u0440\u043e\u0439 \u043d\u0435 \u0432\u044b\u0448\u0435\u043b \u0437\u0430 \u043f\u0440\u0435\u0434\u0435\u043b\u044b \u043f\u043e\u043b\u044f<\/p>\n<h2>\u0418\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435, \u043a\u0430\u043a \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435<\/h2>\n<p>\u041c\u043e\u043d\u0430\u0434\u044b \u0432 Haskell \u044d\u0442\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0441 \u043f\u043e\u0431\u043e\u0447\u043d\u044b\u043c \u044d\u0444\u0444\u0435\u043a\u0442\u043e\u043c. \u041f\u043e\u0431\u043e\u0447\u043d\u044b\u0439 \u044d\u0444\u0444\u0435\u043a\u0442 \u043c\u043e\u043d\u0430\u0434\u044b State &#8212; \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435:<\/p>\n<pre><code class=\"haskell\">State s a, \u0433\u0434\u0435 s - \u043a\u0430\u043a\u043e\u0435-\u043b\u0438\u0431\u043e \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435,                 a - \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c\u043e\u0435 \u043a\u0430\u043a\u0438\u043c \u0442\u043e \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0438\u0437 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<\/code><\/pre>\n<p>\u041e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<pre><code class=\"haskell\">heroMove :: Char -> State GameField () heroMove ch = modify (move ch)<\/code><\/pre>\n<p>\u043e\u043d\u0430 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0441\u0438\u043c\u0432\u043e\u043b, \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u043e\u043d\u0430\u0434\u0443 State, \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435\u043c \u043a\u043e\u0442\u043e\u0440\u043e\u0439 \u0431\u0443\u0434\u0435\u0442 GameField (\u0438\u0433\u0440\u043e\u0432\u043e\u0435 \u043f\u043e\u043b\u0435), \u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 (). \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0441\u0442\u0430: \u0444\u0443\u043d\u043a\u0446\u0438\u044f modify \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044e (s -> s), (GameField -> GameField) \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435.<\/p>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u043c \u0444\u0443\u043d\u043a\u0446\u0438\u044e<\/p>\n<pre><code class=\"haskell\">pathMove :: String -> State GameField () pathMove = mapM_ heroMove<\/code><\/pre>\n<p>\u042d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0431\u0443\u0434\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0441\u0442\u0440\u043e\u043a\u0443, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e \u0441\u0443\u0442\u0438 \u0431\u0443\u0434\u0435\u0442 \u043f\u0443\u0442\u0435\u043c, \u043f\u043e \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043f\u0440\u043e\u0439\u0434\u0435\u0442 \u043d\u0430\u0448 \u0433\u0435\u0440\u043e\u0439. \u0420\u0435\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u0430 \u044d\u0442\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0447\u0435\u0440\u0435\u0437<\/p>\n<pre><code class=\"haskell\">mapM_ :: (Foldable t, Monad m) => (a -> m b) -> t a -> m () -- \u0430 \u0435\u0441\u043b\u0438 \u0431\u0435\u0437 \u043f\u043e\u043b\u0438\u043c\u043e\u0440\u0444\u0438\u0437\u043c\u0430 mapM_ :: (Char -> State GameField ()) -> String -> State GameField ()<\/code><\/pre>\n<p>\u0412\u0441\u0435 \u0433\u043e\u0442\u043e\u0432\u043e, \u0447\u0442\u043e\u0431\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u0443\u044e \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u0443\u044e \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u0432\u0432\u0435\u0434\u0435\u0442 \u043f\u0443\u0442\u044c \u0438 \u043f\u043e\u043b\u0443\u0447\u0438\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u041f\u043e\u043b\u043d\u044b\u0439 \u043b\u0438\u0441\u0442\u0438\u043d\u0433 \u0444\u0430\u0439\u043b\u0430 GameFieldState.hs:<\/p>\n<pre><code class=\"haskell\">module GameFieldState where  import           Control.Monad.State import           GameField  heroMove :: Char -> State GameField () heroMove ch = modify (move ch)  pathMove :: String -> State GameField () pathMove = mapM_ heroMove  main :: IO () main = do   let gf = GameField 3 (2, 2) -- \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f   print gf                    -- \u0432\u044b\u0432\u043e\u0434 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f   path &lt;- getLine             -- \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u043e\u0433\u043e \u0432\u0432\u043e\u0434\u0430   print $ execState (pathMove path) gf -- \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0438 \u0432\u044b\u0432\u043e\u0434 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f<\/code><\/pre>\n<p>\u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432 ghci:<\/p>\n<pre><code class=\"haskell\">:l GameFieldState main OOO OXO OOO  WA XOO OOO OOO<\/code><\/pre>\n<p>\u0412\u0432\u0435\u0434\u044f \u0441\u0442\u0440\u043e\u043a\u0443 WA, \u043c\u044b &#171;\u0437\u0430\u0433\u043d\u0430\u043b\u0438&#187; \u0433\u0435\u0440\u043e\u044f \u0432 \u0432\u0435\u0440\u0445\u043d\u0438\u0439 \u043b\u0435\u0432\u044b\u0439 \u0443\u0433\u043e\u043b.<\/p>\n<h2>StateT &#8212; \u0435\u0449\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0438\u043d\u0442\u0435\u0440\u0430\u043a\u0442\u0438\u0432\u043d\u043e\u0441\u0442\u0438<\/h2>\n<p>\u041d\u043e \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0432\u0432\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0432\u0435\u0441\u044c \u043f\u0443\u0442\u044c \u0441\u0440\u0430\u0437\u0443, \u0430 \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u043c \u0448\u0430\u0433\u043e\u043c \u0433\u0435\u0440\u043e\u044f \u0438 \u0432\u0438\u0434\u0435\u0442\u044c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0435\u0440\u0430\u043c \u043c\u043e\u043d\u0430\u0434 \u044d\u0442\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e.<\/p>\n<p>\u0422\u0438\u043f \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u043f\u0435\u0440\u0435\u0434\u0432\u0438\u0436\u0435\u043d\u0438\u044f \u0433\u0435\u0440\u043e\u044f \u0431\u0443\u0434\u0435\u0442 \u0443\u0436\u0435 \u0442\u0430\u043a\u043e\u0439:<\/p>\n<pre><code class=\"haskell\">heroMove :: StateT GameField IO ()<\/code><\/pre>\n<p>\u0423\u0436\u0435 \u043d\u0435\u0442 \u0441\u0438\u043c\u0432\u043e\u043b\u0430(Char) \u0442\u043e\u043b\u044c\u043a\u043e<\/p>\n<pre><code class=\"haskell\">StateT s m a, s - \u0442\u0430\u043a\u0436\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435               a - \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435               m - \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u044f\u044f \u043c\u043e\u043d\u0430\u0434\u0430<\/code><\/pre>\n<p>IO \u0431\u0443\u0434\u0435\u0442 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0439 \u043c\u043e\u043d\u0430\u0434\u043e\u0439. \u0424\u0430\u0439\u043b GameFieldStateT.hs \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a<\/p>\n<pre><code class=\"haskell\">module GameFieldStateT where  import           Control.Monad.Trans import           Control.Monad.Trans.State import           GameField  heroMove :: StateT GameField IO () heroMove = do   gf &lt;- get           -- \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0438\u0433\u0440\u043e\u0432\u043e\u0433\u043e \u043f\u043e\u043b\u044f   lift $ print gf     -- \u0432\u044b\u0432\u043e\u0434, lift \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u044c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 IO    ch &lt;- lift getChar  -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0441\u0438\u043c\u0432\u043e\u043b \u043e\u0442 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f   lift $ putStrLn \"\"  -- \u043d\u043e\u0432\u0430\u044f \u0441\u0442\u0440\u043e\u043a\u0430 \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435   modify $ move ch    -- \u0443\u0436\u0435 \u0437\u043d\u0430\u043a\u043e\u043c\u0430\u044f modify \u0438 move ch   heroMove            -- \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0439 \u0432\u044b\u0437\u043e\u0432, \u043d\u0435\u0442 \u043f\u043e\u043a\u043e\u044f \u0433\u0435\u0440\u043e\u044e  main :: IO () main = evalStateT heroMove (GameField 4 (2, 2)) -- \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u044f \u0432 StateT<\/code><\/pre>\n<p>\u0418\u0441\u043f\u044b\u0442\u0430\u0435\u043c \u0432 ghci:<\/p>\n<pre><code class=\"haskell\">:l GameFieldStateT main OOOO OXOO OOOO OOOO  d OOOO OXOO OOOO OOOO  D OOOO OOXO OOOO OOOO  S OOOO OOOO OOXO OOOO<\/code><\/pre>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0412 \u0441\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f, \u043b\u0438\u0447\u043d\u043e \u043c\u0435\u043d\u044f \u043e\u0447\u0435\u043d\u044c \u043f\u0443\u0433\u0430\u043b\u0430 \u043c\u043e\u043d\u0430\u0434\u0430 State. \u0422\u043e\u0433\u0434\u0430 \u043c\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043f\u0440\u0438\u0433\u043e\u0434\u0438\u043b\u0441\u044f \u0431\u044b \u043f\u0440\u0438\u043c\u0435\u0440 \u0435\u0435 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f. \u0412\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u0432\u0438\u0434\u0438\u0448\u044c, \u043a\u0430\u043a \u0447\u0442\u043e-\u0442\u043e \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0435 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0435\u0442\u0441\u044f \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435, \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043f\u0440\u043e\u0449\u0435 \u043f\u043e\u043d\u044f\u0442\u044c \u044d\u0442\u043e \u0441\u0430\u043c\u043e\u0435 \u043d\u0435\u043f\u043e\u043d\u044f\u0442\u043d\u043e\u0435. \u041d\u0430\u0434\u0435\u044e\u0441\u044c \u0434\u0430\u043d\u043d\u0430\u044f \u0441\u0442\u0430\u0442\u044c\u044f \u043a\u043e\u043c\u0443-\u0442\u043e \u043f\u043e\u043c\u043e\u0436\u0435\u0442.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/694944\/\"> https:\/\/habr.com\/ru\/post\/694944\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-340120","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/340120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=340120"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/340120\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=340120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=340120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=340120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}