{"id":178311,"date":"2013-04-29T11:23:03","date_gmt":"2013-04-29T07:23:03","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=178311"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=178311","title":{"rendered":"<span class=\"post_title\">\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u0442\u0435\u043a\u0430 \u0437\u0430 \u0441\u0447\u0451\u0442 \u2026 \u0441\u0442\u0435\u043a\u0430 \u0432\u044b\u0437\u043e\u0432\u043e\u0432<\/span>"},"content":{"rendered":"<div class=\"content html_format\"> \t\t\t\u041f\u0440\u0438\u0448\u043b\u0430 \u043c\u043d\u0435 \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u0438\u0434\u0435\u044f: \u0435\u0441\u0442\u044c \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438 \u0441\u0442\u0435\u043a \u043a\u0430\u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u0410 \u043d\u0435\u043b\u044c\u0437\u044f \u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445?<br \/>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u0430\u0437\u043c\u044b\u0441\u043b\u0438\u0432 \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0432\u043e \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0431\u0435\u043a\u0438 (\u0432\u0435\u0434\u044c \u043f\u043e\u043a\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0442\u0435\u043a\u0435, \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0435\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438). \u0412\u043e \u0432\u0442\u043e\u0440\u044b\u0445 \u0434\u043e\u0441\u0442\u0443\u043f \u0441\u0442\u0440\u043e\u0433\u043e LIFO.<br \/>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u044f\u0437\u044b\u043a\u0430 \u044f \u0440\u0435\u0448\u0438\u043b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c Nemerle \u2014 \u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0439 \u043c\u043d\u0435 .NET + \u0443\u0434\u043e\u0431\u0441\u0442\u0432\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u0441\u0442\u0438\u043b\u044f.<\/p>\n<p>  \u0421\u0430\u043c\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0432\u043d\u0435\u0448\u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u0430:  <\/p>\n<pre><code>variant StackAction[T]{   | Push{     value : T;     next : void -&gt; StackAction[T];   }   | Pop {     next : T -&gt; StackAction[T];   } }  module StackStack{   public Enter[T](current : StackAction[T].Push) : StackAction[T]{     def processNext(next : StackAction[T]): StackAction[T]{       match(next){       | push is StackAction[T].Push =&gt; processNext(Enter(push));       | pop is StackAction[T].Pop =&gt; pop.next(current.value);       }     }      processNext(current.next());   }   } <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u043d\u0435\u0437\u043d\u0430\u043a\u043e\u043c\u044b\u0445 \u0441 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441\u043e\u043c Nemerle:<br \/>  variant StackAction[T] \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u043d\u044b\u0439 generic \u0442\u0438\u043f, \u043f\u0435\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043b\u0438\u0431\u043e Push, \u043b\u0438\u0431\u043e Pop. \u041f\u0440\u0438\u0447\u0451\u043c \u0435\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 Push, \u0442\u043e \u0434\u043e\u043b\u0436\u043d\u044b \u0431\u044b\u0442\u044c \u043f\u043e\u043b\u044f value (\u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043c\u044b \u043a\u043b\u0430\u0434\u0451\u043c \u043d\u0430 \u0441\u0442\u0435\u043a), \u0438 next \u2014 \u043a\u043e\u043b\u0431\u044d\u043a, \u0441 \u043f\u0443\u0441\u0442\u044b\u043c \u0441\u043f\u0438\u0441\u043a\u043e\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 StackAction. \u0410 \u0435\u0441\u043b\u0438 Pop, \u0442\u043e \u0443 \u043d\u0435\u0433\u043e \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043a\u043e\u043b\u0431\u0435\u043a \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u044e\u0449\u0438\u0439 \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0441\u0442\u0435\u043a\u0430, \u0438 \u0442\u0430\u043a\u0436\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u044e\u0449\u0438\u0439 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 StackAction.<\/p>\n<p>  \u0424\u0443\u043d\u043a\u0446\u0438\u044f Enter \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u0441\u0430\u043c\u0443 \u043b\u043e\u0433\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441\u043e \u0441\u0442\u0435\u043a\u043e\u043c. \u0412\u043d\u0443\u0442\u0440\u0438 \u043d\u0435\u0451 \u043e\u0431\u044a\u044f\u0432\u043b\u0435\u043d\u0430 \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f processNext, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 current \u0432 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f. \u0422\u0435\u043b\u043e processNext \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u0438\u0437 \u043e\u0434\u043d\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0430 match (\u0441\u043e\u043f\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0441 \u043e\u0431\u0440\u0430\u0437\u0446\u043e\u043c). push \u0438 pop \u0441\u0438\u043d\u043e\u043d\u0438\u043c\u044b next \u0432 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a\u043e\u0439 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0439 \u0442\u0438\u043f \u043f\u0440\u0438\u043c\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u043b\u043e\u0433\u0438\u043a\u0430 \u0440\u0430\u0431\u043e\u0442\u044b Enter: \u0432\u044b\u0437\u0432\u0430\u0442\u044c current.next \u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u0442\u044c \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 processNext, \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0438\u0437 processNext \u0437\u043d\u0430\u0447\u043d\u0438\u0435 \u0432\u0435\u0440\u043d\u0443\u0442\u044c. (\u0412 nemerle <i>\u043d\u0435\u0442<\/i> \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 return, \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438\u0437 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f, \u0430 match \u0438\u0437 \u0432\u044b\u043f\u043e\u043b\u0435\u043d\u043d\u043e\u0439 \u0432\u0435\u0442\u043a\u0438) <br \/>  processNext \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0435 \u0435\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435. \u0415\u0441\u043b\u0438 Push, \u0442\u043e \u043e\u043d\u0430 \u0432\u044b\u0437\u0432\u0430\u0435\u0442 \u0441 \u044d\u0442\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c Enter, \u0430 \u0441 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u043c \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f Enter \u2014 \u0441\u0435\u0431\u044f. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0446\u0438\u043a\u043b \u2014 \u043f\u043e\u043a\u0430 \u043a\u043e\u043b\u0431\u0435\u043a \u043d\u0435 \u0432\u0435\u0440\u043d\u0451\u0442 Pop, \u0432\u044b\u0445\u043e\u0434\u0430 \u0438\u0437 \u0442\u0435\u043a\u0443\u0449\u0435\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430 Enter \u043d\u0435 \u0431\u0443\u0434\u0435\u0442. \u0415\u0441\u043b\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 next \u2014 Pop, \u0442\u043e \u0432 \u043a\u043e\u043b\u0431\u0435\u043a \u043f\u0435\u0440\u0435\u0434\u0430\u0451\u0442\u0441\u044f \u0438\u0437 \u0437\u0430\u043c\u044b\u043a\u0430\u043d\u0438\u044f \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 current.value (\u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u0441\u0430\u043c\u0430 processNext \u043c\u043e\u0433\u043b\u0430 \u0443\u0436\u0435 \u0431\u044b\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0440\u0430\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u0432\u044b\u0437\u0432\u0430\u043d\u0430).<\/p>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u0435\u0449\u0451 \u043e\u0434\u0438\u043d \u043d\u0435\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u043a: \u0435\u0441\u043b\u0438 Pop \u0432\u043e\u0437\u044c\u043c\u0451\u0442 \u0441\u043e \u0441\u0442\u0435\u043a\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0438 \u0441\u0442\u0435\u043a \u043e\u043f\u0443\u0441\u0442\u0435\u0435\u0442, \u0442\u043e \u0432\u044b\u0437\u0432\u0430\u043d\u043d\u044b\u0439 \u0432 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u043c \u043a\u043e\u0434\u0435 Enter \u0432\u0435\u0440\u043d\u0451\u0442 \u0442\u043e, \u0447\u0442\u043e \u0432\u0435\u0440\u043d\u0443\u043b \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 Pop. \u0422\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u0436\u043d\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0441\u0442\u0435\u043a\u0430 \u043d\u0443\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0446\u0438\u043a\u043b.<\/p>\n<p>  \u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0432\u043e\u0437\u044c\u043c\u0451\u043c \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u0432 <a href=\"http:\/\/ru.wikipedia.org\/wiki\/%D0%9E%D0%B1%D1%80%D0%B0%D1%82%D0%BD%D0%B0%D1%8F_%D0%BF%D0%BE%D0%BB%D1%8C%D1%81%D0%BA%D0%B0%D1%8F_%D0%B7%D0%B0%D0%BF%D0%B8%D1%81%D1%8C\">\u043e\u0431\u0440\u0430\u0442\u043d\u043e\u0439 \u043f\u043e\u043b\u044c\u0441\u043a\u043e\u0439 \u043d\u043e\u0442\u0430\u0446\u0438\u0438<\/a>.<\/p>\n<pre><code>def items =  &quot;7 2 3 * -&quot;.Split(array[' ']); mutable currentPosition = 0;  def processNextToken(){   def action(operation : double * double -&gt; double){     StackAction.Pop(fun(y){       StackAction.Pop(fun(x){         StackAction.Push(operation(x, y), processNextToken);       });     });   }    if(currentPosition &gt;= items.Length){     StackAction.Pop(fun(x){       StackAction.Push(x, fun(){throw Exception(&quot;Bad input, not enough operations.&quot;)});     });   }else{     currentPosition++;     mutable value : double;     match(items[currentPosition-1]){     | strVal when (Double.TryParse(strVal, out value)) =&gt; StackAction.Push(value, processNextToken);     | &quot;+&quot; =&gt; action(_ + _);     | &quot;-&quot; =&gt; action(_ - _);     | &quot;\/&quot; =&gt; action(_ \/ _);     | &quot;*&quot; =&gt; action(_ * _);     | token =&gt; throw Exception($&quot;bad token $token&quot;);     }   } }   def calc(current : StackAction[double]){   match(current){   | StackAction.Push (_, next) when (next == processNextToken)        =&gt; calc(StackStack.Enter(current :&gt; StackAction[double].Push));   | StackAction.Push (res, _) =&gt; WriteLine($&quot;Result = $res&quot;);   | StackAction.Pop =&gt; throw Exception(&quot;Bad input, not enough arguments.&quot;);   } }  calc(processNextToken()); <\/code><\/pre>\n<p>  \u041a\u0440\u0430\u0442\u043a\u043e\u0435 \u043f\u043e\u044f\u0441\u043d\u0435\u043d\u0438\u0435 \u043d\u0430\u0447\u0438\u043d\u0430\u044f \u0441 \u043a\u043e\u043d\u0446\u0430:<br \/>  calc \u0440\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u0442 \u043b\u043e\u0433\u0438\u043a\u0443 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u0436\u043d\u0438\u043c \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u043e\u043c \u0441\u0442\u044d\u043a\u0430: \u0435\u0441\u043b\u0438 \u0432\u044b\u0432\u0430\u043b\u0438\u043b\u0441\u044f Push \u0441 \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u043c processNextToken \u0442\u043e \u0441\u043d\u043e\u0432\u0430 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c calc, \u0435\u0441\u043b\u0438 \u0432\u044b\u0432\u0430\u043b\u0438\u043b\u0441\u044f Push \u0441 \u0434\u0440\u0443\u0433\u0438\u043c \u043a\u043e\u043b\u0431\u0435\u043a\u043e\u043c (fun(){throw Exception(\u00abBad input, not enough operations.\u00bb)), \u0437\u043d\u0430\u0447\u0438\u0442 \u0432\u0441\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0430\u043d\u0430 \u0438 \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0432\u0435\u0440\u043d\u0443\u043b\u0430 \u043a\u043e\u043d\u0435\u0447\u043d\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442. \u0415\u0441\u043b\u0438 \u0432\u044b\u0432\u0430\u043b\u0438\u043b\u0441\u044f Pop, \u0437\u043d\u0430\u0447\u0438\u0442 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u043c\u0443 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044e \u043d\u0435 \u0445\u0432\u0430\u0442\u0438\u043b\u043e \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u0432.<\/p>\n<p>  processNextToken \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0442\u043e\u043a\u0435\u043d\u044b \u043f\u043e \u043f\u043e\u0440\u044f\u0434\u043a\u0443. \u0415\u0441\u043b\u0438 \u0434\u043e\u0441\u0442\u0438\u0433\u043d\u0443\u0442 \u043a\u043e\u043d\u0435\u0446 \u0437\u0430\u043f\u0438\u0441\u0438, \u0431\u0435\u0440\u0451\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0441\u043e \u0441\u0442\u0435\u043a\u0430 \u0438 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u0435\u0433\u043e \u0432 calc. \u0415\u0441\u043b\u0438 \u043d\u0430 \u0441\u0442\u0435\u043a\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 1 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0437\u0432\u0430\u043d\u0430 \u0430\u043d\u043e\u043d\u0438\u043c\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f fun(){throw Exception(\u00abBad input, not enough operations.\u00bb)}. \u0415\u0441\u043b\u0438 \u0435\u0441\u0442\u044c \u0435\u0449\u0451 \u0442\u043e\u043a\u0435\u043d\u044b, \u0431\u0435\u0440\u0451\u043c \u0442\u0435\u043a\u0443\u0449\u0438\u0439. \u0427\u0438\u0441\u043b\u043e\u0432\u043e\u0439 \u043b\u0438\u0442\u0435\u0440\u0430\u043b \u043a\u043b\u0430\u0434\u0451\u043c \u043d\u0430 \u0441\u0442\u0435\u043a, \u0434\u043b\u044f \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0445 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c action. \u0417\u0430\u043f\u0438\u0441\u0438 _ + _ \u2014 \u043e\u0441\u043e\u0431\u0430\u044f \u043c\u0430\u0433\u0438\u044f nemerle \u2014 \u0447\u0430\u0441\u0442\u0438\u0447\u043d\u043e\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435. \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0430\u0440\u0438\u0444\u043c\u0435\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0432 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0441 \u0434\u0432\u0443\u043c\u044f \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438.<\/p>\n<p>  action \u0431\u0435\u0440\u0451\u0442 2 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u043e \u0441\u0442\u0435\u043a\u0430, \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0441 \u043d\u0438\u043c\u0438 \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u0443\u044e \u0435\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u044e \u0438 \u043a\u043b\u0430\u0434\u0451\u0442 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u043d\u0430 \u0441\u0442\u0435\u043a.<\/p>\n<p>  \u0414\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0437\u0430\u043f\u0443\u0442\u0430\u043d\u043e \u043f\u0440\u0430\u0432\u0434\u0430? \u041c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043b\u0430\u0441\u0441 \u0441 \u043f\u0440\u0438\u0432\u044b\u0447\u043d\u044b\u043c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u043e\u043c \u0441\u0442\u0435\u043a\u0430, \u0435\u0441\u043b\u0438 \u043f\u0435\u0440\u0435\u043d\u0435\u0441\u0442\u0438 \u0441\u0442\u0435\u043a, \u0445\u0440\u0430\u043d\u044f\u0449\u0438\u0439 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u043f\u043e\u0442\u043e\u043a.<\/p>\n<pre><code>enum Action{   | Push   | Pop }  public class StackEmptyException : Exception{   public this(message : string){     base(message);   } }  public class ThreadStack[T] : IDisposable{    class Resident{             public mutable volatile action : Action;     public mutable volatile value : T;     public mutable volatile exception : Exception;     public syncIn = AutoResetEvent(false);     public syncOut = AutoResetEvent(false);      public start() : void{       exception = null;       try{         mutable current = next();         while(true){           match(current){           | act is StackAction[T].Push =&gt; current = StackStack.Enter(act : StackAction[T].Push);           | StackAction.Pop =&gt; throw StackEmptyException(&quot;Stack is empty&quot;);                               }                             }       }catch{       | e =&gt; {exception = e; _ = syncOut.Set();}       }     }      next() : StackAction[T]{       _ = syncOut.Set();       _ = syncIn.WaitOne();       match(action){       | Push =&gt; StackAction.Push(value, next);       | Pop =&gt; StackAction.Pop(fun(x){         value = x;         next();});       }     }   }    private resident : Resident = Resident();   private thread : Thread;    public this(){     thread = Thread(ThreadStart(resident.start));     thread.Start();   }    public Dispose() : void implements IDisposable.Dispose {     try{       thread.Abort();       _ = resident.syncIn.Set();       thread.Join();       (resident.syncIn : IDisposable).Dispose();       (resident.syncOut : IDisposable).Dispose();     }finally{}   }    private checkException() : void{     when(resident.exception != null) {       throw resident.exception;     }   }    private exec() : void{     _ = resident.syncIn.Set();     _ = resident.syncOut.WaitOne();     checkException();   }    public Push(val : T) : void{     resident.value = val;     resident.action = Action.Push;     exec();   }    public Pop() : T{             resident.action = Action.Pop;     exec();     resident.value;   } } <\/code><\/pre>\n<p>  \u0418 \u0445\u043e\u0442\u044f \u043a\u043e\u0434\u0430 \u0433\u043e\u0440\u0430\u0437\u0434\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u044f \u0434\u0443\u043c\u0430\u044e \u043e\u043d \u0443\u0436\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u043f\u043e\u043d\u044f\u0442\u0435\u043d \u0442\u0435\u043c, \u043a\u0442\u043e \u0437\u043d\u0430\u0435\u0442 C#. \u0415\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0435: \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044f &quot;_ =&quot; \u0441\u043e\u043e\u0431\u0449\u0430\u0435\u0442 \u043a\u043e\u043c\u043f\u0438\u043b\u044f\u0442\u043e\u0440\u0443, \u0447\u0442\u043e \u043c\u044b \u0438\u0433\u043d\u043e\u0440\u0438\u0440\u0443\u0435\u043c \u0432\u043e\u0437\u0432\u0430\u0440\u0430\u0449\u0430\u0435\u043c\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u0410 \u0432\u043e\u0442 \u0438 \u0442\u0430 \u0436\u0435 \u043e\u0431\u0440\u0430\u0442\u043d\u0430\u044f \u043f\u043e\u043b\u044c\u0441\u043a\u0430\u044f \u043d\u043e\u0442\u0430\u0446\u0438\u044f:  <\/p>\n<pre><code>def items =  &quot;7 2 3 * -&quot;.Split(array[' ']); mutable currentPosition = 0;  mutable stack : ThreadStack[double]; try{   stack = ThreadStack();   mutable onStack = 0;   def execOperation(operation : double * double -&gt; double){     def y = stack.Pop();     def x = stack.Pop();     stack.Push(operation(x, y));     onStack--;   }    currentPosition = 0;               while(currentPosition &lt; items.Length){     currentPosition++;     mutable value : double;     match(items[currentPosition-1]){     | strVal when (Double.TryParse(strVal, out value)) =&gt; { onStack++; stack.Push(value);}     | &quot;+&quot; =&gt; execOperation(_ + _);     | &quot;-&quot; =&gt; execOperation(_ - _);     | &quot;\/&quot; =&gt; execOperation(_ \/ _);     | &quot;*&quot; =&gt; execOperation(_ * _);     | token =&gt; throw Exception($&quot;bad token $token&quot;);     }   }   when(onStack &gt; 1){     throw Exception(&quot;Bad input, not enough operations.&quot;);   }   WriteLine(&quot;Result: &quot; + stack.Pop());             }catch{   | e is StackEmptyException =&gt; throw Exception(&quot;Bad input, not enough arguments.&quot;); }finally{   stack.Dispose(); } <\/code><\/pre>\n<p>  \u041d\u0443 \u0438 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0441\u0442\u0430\u0442\u044c\u0435 \u043d\u0443\u0436\u0435\u043d \u0432\u044b\u0432\u043e\u0434: \u0434\u0430\u0436\u0435 \u0442\u0430\u043a\u0438\u0435 \u0438\u0437\u0432\u0440\u0430\u0449\u0435\u043d\u0438\u044f \u043d\u0430 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u043c \u044f\u0437\u044b\u043a\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u044e\u0442\u0441\u044f \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0451\u043c\u043a\u0438\u043c\u0438 \u0438 \u043f\u043e\u043d\u044f\u0442\u043d\u044b\u043c\u0438 (\u0435\u0441\u043b\u0438 \u0438\u043c\u0435\u0442\u044c \u043d\u0430\u0432\u044b\u043a \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u043d\u0438\u043c\u0438). \u0418\u043c\u043f\u0435\u0440\u0430\u0442\u0438\u0432\u043d\u044b\u0439 \u0441\u0442\u0438\u043b\u044c \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u0435\u0435, \u043d\u043e \u0432\u0441\u0451 \u0436\u0435 \u0434\u043b\u044f \u043d\u0435\u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u044f \u043f\u043e\u043d\u044f\u0442\u043d\u0435\u0435. \t\t\t \t\t\t<\/p>\n<div class=\"clear\"><\/div>\n<\/p><\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"http:\/\/habrahabr.ru\/post\/178311\/\"> http:\/\/habrahabr.ru\/post\/178311\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\"> \t\t\t\u041f\u0440\u0438\u0448\u043b\u0430 \u043c\u043d\u0435 \u043e\u0434\u043d\u0430\u0436\u0434\u044b \u0438\u0434\u0435\u044f: \u0435\u0441\u0442\u044c \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0438 \u0441\u0442\u0435\u043a \u043a\u0430\u043a \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445. \u0410 \u043d\u0435\u043b\u044c\u0437\u044f \u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0441\u0442\u0435\u043a \u0432\u044b\u0437\u043e\u0432\u043e\u0432 \u0434\u043b\u044f \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u044b\u0445?<br \/>  \u041d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u0440\u0430\u0437\u043c\u044b\u0441\u043b\u0438\u0432 \u044f \u043f\u043e\u043d\u044f\u043b, \u0447\u0442\u043e \u043c\u043e\u0436\u043d\u043e, \u0442\u043e\u043b\u044c\u043a\u043e \u0441 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u044f\u043c\u0438: \u0432\u043e \u043f\u0435\u0440\u0432\u044b\u0445 \u0434\u043b\u044f \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0439 \u043f\u0440\u0438\u0434\u0451\u0442\u0441\u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u043b\u0431\u0435\u043a\u0438 (\u0432\u0435\u0434\u044c \u043f\u043e\u043a\u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043d\u0430 \u0441\u0442\u0435\u043a\u0435, \u043d\u0435\u043b\u044c\u0437\u044f \u0432\u044b\u0445\u043e\u0434\u0438\u0442\u044c \u0438\u0437 \u0442\u043e\u0433\u043e \u0432\u044b\u0437\u043e\u0432\u0430, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043c\u044b \u0435\u0433\u043e \u0441\u043e\u0445\u0440\u0430\u043d\u0438\u043b\u0438). \u0412\u043e \u0432\u0442\u043e\u0440\u044b\u0445 \u0434\u043e\u0441\u0442\u0443\u043f \u0441\u0442\u0440\u043e\u0433\u043e LIFO.<br \/>  \u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u2014 \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.  <\/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-178311","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/178311","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=178311"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/178311\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=178311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=178311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=178311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}