wtd
|
Posted: Mon Jul 03, 2006 12:02 pm Post subject: An Open Challenge |
|
|
For those looking for a language to learn who aren't enthralled with nostalgia by my recent C and Pascal tours, give Erlang a try.
My first Erlang program:
code: | -module(hello).
-export([hello/1]).
-import(io).
hello(Name) ->
io:format("Hello, ~s!~n", [Name]). |
This was tested in the interpreter with:
code: | 1> c(hello).
{ok,hello}
2> hello:hello("wtd").
Hello, wtd!
ok |
And a modification that uses pattern matching:
code: | -module(hello).
-export([hello/1]).
-import(io).
hello("wtd") ->
io:format("That cur!~n", []) ;
hello(Name) ->
io:format("Hello, ~s!~n", [Name]). |
A little more pattern matching, and recursion:
code: | -module(hello).
-export([hello/1, greet/2]).
-import(io).
hello("wtd") ->
io:format("That cur!~n", []) ;
hello(Name) ->
io:format("Hello, ~s!~n", [Name]).
greet(_, []) ->
{ok} ;
greet(Greeting, ["wtd"|Rest]) ->
hello("wtd") ,
greet(Greeting, Rest) ;
greet(Greeting, [First|Rest]) ->
io:format("~s, ~s!~n", [Greeting, First]) ,
greet(Greeting, Rest). |
Some first-class functions:
code: | Factorial = fun(N) ->
lists:foldl(fun(A, B) ->
A * B
end,
1, lists:seq(1, N))
end. |
|
|
|