Tutorial by Examples

Here the simplest recursive function over list type. This function only navigate into a list from its start to its end and do nothing more. -spec loop(list()) -> ok. loop([]) -> ok; loop([H|T]) -> loop(T). You can call it like this: loop([1,2,3]). % will return ok. Here the ...
Like list, simplest function over iolist and bitstring is: -spec loop(iolist()) -> ok | {ok, iolist} . loop(<<>>) -> ok; loop(<<Head, Tail/bitstring>>) -> loop(Tail); loop(<<Rest/bitstring>>) -> {ok, Rest} You can call it like this: lo...

Map

Map in Erlang is equivalent of hashes in Perl or dictionaries in Python, its a key/value store. To list every value stored in, you can list every key, and return key/value pair. This first loop give you an idea: loop(Map) when is_map(Map) -> Keys = maps:keys(Map), loop(Map, Keys). loop...
Recursive function use their states to loop. When you spawn new process, this process will be simply a loop with some defined state.
Here 2 examples of recursive anonymous functions based on previous example. Firstly, simple infinite loop: InfiniteLoop = fun R() -> R() end. Secondly, anonymous function doing loop over list: LoopOverList = fun R([]) -> ok; R([H|T]) -> R(T) end. These two func...

Page 1 of 1