Revision of June 2008 irc log from Sun, 06/01/2008 - 15:27

14:54 <@nem> nem(14:43:41) nem@aegir erlirc $ erl
14:54 <@nem> Erlang (BEAM) emulator version 5.6.2 [source] [smp:2] [async-threads:0] [kernel-poll:false]
14:54 <@nem> Eshell V5.6.2 (abort with ^G)
14:54 <@nem> 1> hello_world.
14:54 <@nem> hello_world
14:54 <@nem> so lets go a little more impressive
14:54 < Shiny> that's cheating
14:54 < waawaamilk> like a program that prints hello world and can NEVER BE KILLED
14:54 < waawaamilk> ?
14:54 <@nem> RLoop = fun (F) ->
14:54 <@nem> receive
14:54 <@nem> hello -> io:format("Got anonymous hello world.~n", []);
14:54 <@nem> {hello_from, Sender, please_greet, Who} -> Sender ! "Hello " ++ term_to_list(Who);
14:54 <@nem> stop -> exit(bye)
14:54 <@nem> end,
14:54 <@nem> F(F)
14:55 <@nem> end.
14:55 < waawaamilk> RLoop = fun eh
14:55 < waawaamilk> erlang = fun!
14:55 * waawaamilk understands
14:55 <@nem> http://paste.lisp.org/display/61520
14:56 <@nem> 11> Greeter = spawn(fun () -> RLoop(RLoop)end).
14:56 <@nem> <0.49.0>
14:56 <@nem> so now I've spawned another process to run Rloop
14:56 < waawaamilk> view source on that pastie
14:56 < waawaamilk> fark
14:56 < waawaamilk> Rloop waits for messages. If it gets 'hello' it prints Got anonymous hello world etc
14:56 < waawaamilk> > > ...
14:57 <@nem> the <0.49.0> thing is the process id of the greeter process
14:57 <@nem> we can tell it to greet stuff now
14:57 <@nem> 12> Greeter ! hello.
14:57 <@nem> Got anonymous hello world.
14:57 <@nem> hello
14:58 <@nem> Got anonymous hello world.
14:59 <@nem> where we send the result of sending hello to the greeter to the greeter
14:59 <@nem> anywho
14:59 <@nem> 14> self().
14:59 <@nem> <0.46.0>
14:59 <@nem> that's us (the shell process)
15:00 <@nem> 15> Greeter ! {hello_from, self(), please_greet, "the bacon monkies."}.
15:00 <@nem> {hello_from,<0.46.0>,please_greet,"the bacon monkies."}
15:00 <@nem> 16>
15:00 <@nem> =ERROR REPORT==== 1-Jun-2008::15:00:00 ===
15:00 <@nem> Error in process <0.49.0> with exit value: {undef,[{shell_default,term_to_list,["the bacon
monkies."]},{erl_eval,do_apply,5},{erl_eval,expr,5},{erl_eval,exprs,5}]}
15:00 <@nem> oh noes.
15:00 -!- spork [~spork@219.88.251.36] has joined #shdh
15:00 <@nem> we crashed.
15:00 -!- Sabotabby [~joh@198.48.0.115] has quit [Quit: leaving]
15:00 <@nem> we tried to execute a function that doesn't exist
15:00 -!- Sabotabby [~Sabotabby@198.48.0.115] has joined #shdh
15:01 < waawaamilk> !!!
15:01 <@nem> (term_to_list lives in the erlang module)
15:01 < Shiny> please_greet ?
15:01 < waawaamilk> erlang crashes?
15:01 < waawaamilk> :)
15:01 < Sabotabby> It does when nem is writing it.
15:01 <@nem> well, my half arsed demo process crashed
15:01 <@nem> so let's fix the code
15:01 <@Ned> lets _you_ fix the code ;-)
15:02 < waawaamilk> print "OH_HAI" > helloworld.erlang
15:02 <@nem> http://paste.lisp.org/display/61520#1
15:02 <@nem> ..... Greeter = <0.57.0> % new process id - the old one went away when it crashed
15:03 <@nem> 19> Greeter ! hello.
15:03 <@nem> Got anonymous hello world.
15:03 <@nem> hello
15:03 <@nem> as before
15:03 <@nem> 20> Greeter ! {hello_from, self(), please_greet, "the bacon monkies."}.
15:03 <@nem> {hello_from,<0.46.0>,please_greet,"the bacon monkies."}
15:03 <@nem> hmm, no crash, but no message
15:04 < waawaamilk> http://nigel.mcnie.name/blog/using-git-for-your-sourceforgegoogle-code-p...
15:04 <@nem> that's because the greeter sent us a message back instead of just printing to stdout
15:04 < Shiny> nem: what are the bits in 20> Greeter ! {hello_from, self(), please_greet, "the bacon monkies."}.
15:04 < Shiny> ?
15:04 <@nem> Greeter is the process id of the greeter process
15:04 <@nem> ! means send the right hand side to the left hand side
15:05 <@nem> {hello_from, self(), please_greet, "the bacon monkies."} is a tuple containing the atoms hello_from and please_greet, the string "the bacon monkies." and our own
process id
15:05 <@nem> it's like an array of 4 things
15:06 <@nem> now the greeter sent us a message back - 21> flush().
15:06 <@nem> Shell got "Hello \"the bacon monkies.\""
15:06 <@nem> ok
15:06 <@nem> the flush command receives all the messages in the message queue for our process
15:06 <@nem> and prints them out
15:06 <@nem> so the string we got back from the greeter looks a little ugly
15:06 < halorgium> 4> erlang:process_info(self(), messages).
15:06 < halorgium> {messages,["Hello \"Joe\""]}
15:07 <@nem> ^^ or that
15:08 <@nem> so let's fix the code again
15:08 <@nem> and add a special case where we're sent a string
15:08 <@nem> but before that, let's do the same thing with an atom
15:09 <@nem> 22> Greeter ! {hello_from, self(), please_greet, the_bacon_monkies}.
15:09 <@nem> {hello_from,<0.46.0>,please_greet,the_bacon_monkies}
15:09 <@nem> 23> flush().
15:09 <@nem> Shell got "Hello the_bacon_monkies"
15:09 <@nem> http://paste.lisp.org/display/61520#2
15:09 <@nem> 24> Greeter ! stop.
15:09 <@nem> stop
15:09 <@nem> 25> process_info(Greeter).
15:09 <@nem> undefined
15:10 <@nem> ok, there's an extraneous h in that last example
15:11 < halorgium> or is_process_alive(Greeter).
15:11 < halorgium> i'm the provide other answers guy
15:11 <@nem> I respawned the greeter with the new code - pid is now <0.71.0>
15:11 <@nem> 31> Greeter ! {hello_from, self(), please_greet, the_bacon_monkies}.
15:11 <@nem> {hello_from,<0.46.0>,please_greet,the_bacon_monkies}
15:12 <@nem> 32> Greeter ! {hello_from, self(), please_greet, "the bacon monkies."}.
15:12 <@nem> {hello_from,<0.46.0>,please_greet,"the bacon monkies."}
15:12 <@nem> 33> flush().
15:12 <@nem> Shell got "Hello the_bacon_monkies"
15:12 <@nem> Shell got "Hello the bacon monkies."
15:12 < Shiny> i think the waitress is afraid of the big silent group of laptop users
15:12 <@nem> so now we have a special case for dealing with 'Who' being a string (list)
15:13 <@nem> I think that's enough for now - you get the idea that you can send and receive messages between processes