diff options
author | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
---|---|---|
committer | Trygve Laugstøl <trygvis@inamo.no> | 2024-02-23 07:08:18 +0100 |
commit | 5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e (patch) | |
tree | 982ca2e7f9ac4e8c350dfb5c4f60bcfdfff5afaf /learn-you-some-erlang/dog_fsm.erl | |
parent | 05ae56e5e89abf2993f84e6d52b250131f247c35 (diff) | |
download | erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.gz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.bz2 erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.tar.xz erlang-workshop-5a9cdd3cc89507d4d74f8bded56ce5e037b3b56e.zip |
wip
Diffstat (limited to 'learn-you-some-erlang/dog_fsm.erl')
-rw-r--r-- | learn-you-some-erlang/dog_fsm.erl | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/learn-you-some-erlang/dog_fsm.erl b/learn-you-some-erlang/dog_fsm.erl new file mode 100644 index 0000000..f784933 --- /dev/null +++ b/learn-you-some-erlang/dog_fsm.erl @@ -0,0 +1,42 @@ +-module(dog_fsm). +-export([start/0, squirrel/1, pet/1]). + +start() -> spawn(fun() -> bark() end). + +squirrel(Pid) -> Pid ! squirrel. + +pet(Pid) -> Pid ! pet. + +bark() -> + io:format("Dog says: BARK! BARK!~n"), + receive + pet -> + wag_tail(); + _ -> + io:format("Dog is confused~n"), + bark() + after 2000 -> + bark() + end. + +wag_tail() -> + io:format("Dog wags its tail~n"), + receive + pet -> + sit(); + _ -> + io:format("Dog is confused~n"), + wag_tail() + after 30000 -> + bark() + end. + +sit() -> + io:format("Dog is sitting. Gooooood boy!~n"), + receive + squirrel -> + bark(); + _ -> + io:format("Dog is confused~n"), + sit() + end. |