-
-
Save sgobotta/143bb2adabe862d2fad9972dc3d33740 to your computer and use it in GitHub Desktop.
-module(first). | |
-export([ | |
double/1, | |
mult/2, | |
area/3, | |
square/1, | |
trbl/1, | |
sum/3 | |
]). | |
mult(X,Y) -> | |
X*Y. | |
double(X) -> | |
mult(2,X). | |
area(A,B,C) -> | |
S = sum(A,B,C)/2, | |
math:sqrt(S*(S-A)*(S-B)*(S-C)). | |
square(X) -> | |
mult(X,X). | |
trbl(X) -> | |
mult(3,X). | |
sum(X,Y,Z) -> | |
X+Y+Z. |
-module(second). | |
-import(first,[ | |
square/1, | |
area/3, | |
sum/3 | |
]). | |
-export([ | |
hypotenuse/2, | |
area/2, | |
perimeter/2 | |
]). | |
% given two sides of a tractangle triangle returns the size of the hypotenuse | |
hypotenuse(X,Y) -> | |
math:sqrt(square(X) + square(Y)). | |
% given two sides of a triangle rectangle returns the area | |
area(X,Y) -> | |
H = hypotenuse(X,Y), | |
area(X,Y,H). | |
% given two sides of a tractangle triangle returns the perimeter | |
perimeter(X,Y) -> | |
H = hypotenuse(X,Y), | |
sum(X,Y,H). | |
Another tip, since you're writing documentation: you can start your doc comments with
% @doc
and that would make them visible toedoc
if you later want to generate automatic module docs.
Oh, thanks you very much for your feedback @elbrujohalcon! I've been reading a little bit about documentation, is that documentation rule bound to hex docs or is it more like a generic practise when documenting?
Great solution, I would just recommend you not to use
-import
and just use fully-qualified names for functions likefirst:square/1
.
Yes, I saw other people using fully-qualified names when re-using functions, I wonder what are the pro and cons. At first sight I wonder what would happen to my area/2
implementation if the area/3
function I'm importing had an arity of 2
.
It's a generic practice. Check it out: http://erlang.org/doc/apps/edoc/chapter.html
About importing functions with similar names… I believe that the compiler will tell you that the function is redefined (you should try and see…). But I wrote a long comment on FutureLearn stating why I think it's not wise to use -import
. Basically, it makes code harder to understand and debug. Not super noticeable in small modules, but definitely hideous in large ones.
Ah, sí… podemos hablar en porteño si te cabe, eh? Por mí, todo bien :P
Aaaajáa. A la tarde voy a estar chequeando los mensajes de esa fase sección cuando remote los ejercicios. Esta bastante entretenido! 🎉
Great solution, I would just recommend you not to use
-import
and just use fully-qualified names for functions likefirst:square/1
.Another tip, since you're writing documentation: you can start your doc comments with
% @doc
and that would make them visible toedoc
if you later want to generate automatic module docs.