Skip to content

Instantly share code, notes, and snippets.

@sgobotta
Last active May 5, 2020 22:35
Show Gist options
  • Save sgobotta/143bb2adabe862d2fad9972dc3d33740 to your computer and use it in GitHub Desktop.
Save sgobotta/143bb2adabe862d2fad9972dc3d33740 to your computer and use it in GitHub Desktop.
Gists for the FeatureLearn Erlang course (1.9)
-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).
@elbrujohalcon
Copy link

Great solution, I would just recommend you not to use -import and just use fully-qualified names for functions like first:square/1.
Another tip, since you're writing documentation: you can start your doc comments with % @doc and that would make them visible to edoc if you later want to generate automatic module docs.

@sgobotta
Copy link
Author

sgobotta commented May 5, 2020

Another tip, since you're writing documentation: you can start your doc comments with % @doc and that would make them visible to edoc 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 like first: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.

@elbrujohalcon
Copy link

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.

@elbrujohalcon
Copy link

Ah, sí… podemos hablar en porteño si te cabe, eh? Por mí, todo bien :P

@sgobotta
Copy link
Author

sgobotta commented May 5, 2020

Aaaajáa. A la tarde voy a estar chequeando los mensajes de esa fase sección cuando remote los ejercicios. Esta bastante entretenido! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment