Gus Wirth wrote:
Andrew Lentvorski wrote:
Gus Wirth wrote:

Problem one: The examples immediately start to use function overloading even when the functions are not related except for the fact they happen to be in the same module. To me this is the epitome of stupid instruction as well as bad programming. Not only does it introduce a concept before its time but it serves to confuse and obscure the operation of the program.

I presume you are referring to:

-module(tut2).
-export([convert/2]).

convert(M, inch) ->
    M / 2.54;

convert(N, centimeter) ->
    N * 2.54.


I'll somewhat defend this one (but not the next one, see below). This appears as function overloading to those coming from a different language. I will agree, though, that this should probably be flagged out since it caused confusion.


This is really rule matching and you *MUST* get this concept immediately or you cannot proceed with the language.

Those are *not* different functions. They are two clauses of the *same* function. Note that the first ends with a ";" and the second ends with a "."
[snip]

No, that part I got. It's just a different way of writing IF statements in other languages. Anyone who doubts that should just try describing what it does in English. IF param1 is this AND/OR param2 is this THEN RETURN some_result, ELSE continue_to_next_section. The problem is with this from section 2.9:

-module(tut6).
-export([list_max/1]).

list_max([Head|Rest]) ->
   list_max(Rest, Head).

list_max([], Res) ->
    Res;
list_max([Head|Rest], Result_so_far) when Head > Result_so_far ->
    list_max(Rest, Head);
list_max([Head|Rest], Result_so_far)  ->
    list_max(Rest, Result_so_far).

And this one:

Now an example of this when working with lists - reversing the order of a list:

-module(tut8).

-export([reverse/1]).

reverse(List) ->
    reverse(List, []).

reverse([Head | Rest], Reversed_List) ->
    reverse(Rest, [Head | Reversed_List]);
reverse([], Reversed_List) ->
    Reversed_List.



I'm guessing Erlang takes part of its lineage from Prolog. These code segments look very similar to me. At least, from what little Prolog I still remember...

-Jon

--
[email protected]
http://www.kernel-panic.org/cgi-bin/mailman/listinfo/kplug-lpsg

Reply via email to