challenge 出力の一時停止と再開

起動すると、標準出力に1秒毎に'a'の1文字を出力し続けるプログラムで、 以下の条件を満たすものを「どう書く?」

  • 'q'キーが押されるとプログラムは終了する
  • 出力中に'p'キーが押されると一時停止する
  • 一時停止中に'p'キーが押されると出力を再開する

Posted feedbacks - Erlang

p[enter]しないと止まりません。。。
open_portとかも見てみたのですが、、、誰か教えてください。。。

erl -noshell -s intter -s init stop
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-module(intter).
-export([start/0,puta/1]).

start()->
    P  = spawn(intter,puta,"a"),
    loop(P).

loop(P) ->
    case io:get_chars('',1) of
        "q" ->
            halt();
        I ->
            P ! I,
            loop(P)
    end.

puta(Status)->
    receive
        "p" ->
            case Status of
                "p" ->
                    io:format("a"),
                    puta("a");
                _ ->
                    puta("p")
            end
    after 1000 ->
            case Status of
                "p"->
                    ok;
                _ ->
                    io:format("a")
            end,
            puta(Status)
    end.

Index

Feed

Other

Link

Pathtraq

loading...