diff --git a/.gitignore b/.gitignore index 609d9e3..80cdacb 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ *.o *.pdf .d +*.beam # ignore build systems .ninja_deps diff --git a/pe/afile_client.erl b/pe/afile_client.erl new file mode 100644 index 0000000..a0ee996 --- /dev/null +++ b/pe/afile_client.erl @@ -0,0 +1,25 @@ +-module(afile_client). +-compile([debug_info]). +-export([ls/1, get_file/2, put_file/3]). + +ls(Server) -> + Server ! {self(), list_dir}, + receive + {Server, FileList} -> + FileList + end. + +get_file(Server, File) -> + Server ! {self(), {get_file, File}}, + receive + {Server, Content} -> + Content + end. + +put_file(Server, File, Contents) -> + Server ! {self(), {put_file, File, Contents}}, + receive + {Server, Result} -> + Result + end. + diff --git a/pe/afile_server.erl b/pe/afile_server.erl new file mode 100644 index 0000000..0816219 --- /dev/null +++ b/pe/afile_server.erl @@ -0,0 +1,37 @@ +-module(afile_server). +-compile([debug_info]). +-export([start/1, loop/1]). + +start(Dir) -> + spawn(afile_server, loop, [Dir]). + +%% Three significant points to observer about this code: +%% +%% + Who to reply to: all the received messages contain +%% the variable Client - the PID of the sender. +%% + Use of self(): the PID of the server. +%% + Pattern matching to select the message. +loop(Dir) -> + receive + {Client, list_dir} -> + Client ! {self(), file:list_dir(Dir)}; + {Client, {get_file, File}} -> + Full = filename:join(Dir, File), + Client ! {self(), file:read_file(Full)}; + {Client, {put_file, File, Contents}} -> + Full = filename:join(Dir, File), + Client ! {self(), file:write_file(Full, Contents)}, + {Client, ping} -> + Client ! {self(), pong}; + {Client, _} -> + Client ! {self(), unrecognised_message}; + {_} -> + io:format("unhandled message") + end, + loop(Dir). + +%% Notes: +%% + function names are always lowercased +%% + universal receiver: receive X -> X end. +%% + erlang: use processes to structure the solutions to our problems. +%% + *ATOMS*ARE*LOWER*CASED* diff --git a/pe/geometry.erl b/pe/geometry.erl new file mode 100644 index 0000000..932ab3e --- /dev/null +++ b/pe/geometry.erl @@ -0,0 +1,11 @@ +-module(geometry). +-export([area/1]). + +area({rectangle, Width, Height}) -> + Width * Height; +area({circle, Radius}) -> + 3.14159 * Radius * Radius; +area({square, Side}) -> + Side * Side. + + diff --git a/pe/heads.erl b/pe/heads.erl new file mode 100644 index 0000000..566688a --- /dev/null +++ b/pe/heads.erl @@ -0,0 +1,14 @@ +-module(heads). +-export([len/1]). + +alen([], Count) -> + Count; + +alen([_|T], Count) -> + alen(T, Count+1). + +len([_|T]) -> + alen(T, 1); + +len([]) -> + 0. diff --git a/pe/hello.erl b/pe/hello.erl new file mode 100644 index 0000000..4ce5169 --- /dev/null +++ b/pe/hello.erl @@ -0,0 +1,5 @@ +-module(hello). +-export([start/0]). + +start() -> + io:format("Goodbye, joe.~n"). diff --git a/pe/shop.erl b/pe/shop.erl new file mode 100644 index 0000000..6b43a66 --- /dev/null +++ b/pe/shop.erl @@ -0,0 +1,4 @@ +-module(shop). +-export([cost/1]). + +