Skip to content

Instantly share code, notes, and snippets.

@gomesalexandre
Created May 5, 2020 14:57
Show Gist options
  • Save gomesalexandre/ff5e0c278a4b2642f47a1574be011045 to your computer and use it in GitHub Desktop.
Save gomesalexandre/ff5e0c278a4b2642f47a1574be011045 to your computer and use it in GitHub Desktop.
FutureLearn Erlang - My first erlang program
-module(first).
-export([add/2, double/1, mult/2, area/3, treble/1, square/1]).
add(A,B) ->
A + B.
mult(X,Y) ->
X * Y.
double(X) ->
mult(2, X).
treble(X) ->
mult(3, X).
area(A,B,C) ->
S = (A + B + C) / 2,
math:sqrt(S*(S-A)*(S-B)*(S-C)).
square(X) ->
mult(X,X).
-module(second).
-import(first, [square/1, add/2, mult/2]).
-export([hypothenus/2, area_from_sides/2, perimeter_from_sides/2]).
hypothenus(A,B) ->
square(A) + square(B).
area_from_sides(A,B) ->
mult(A,B) / 2.
perimeter_from_sides(A,B) ->
add(A,B) + (math:sqrt(hypothenus(A,B))).
@elbrujohalcon
Copy link

This is perfect, but I would recommend not to use -import and just write things like first:square(…) and such.

@gomesalexandre
Copy link
Author

This is perfect, but I would recommend not to use -import and just write things like first:square(…) and such.

Thanks! I think with the right IDE support this is not a problem, you can always go to definition.
At least in theory, I didn't manage on mine :( (vim-erlang-runtime)

@elbrujohalcon
Copy link

Yeah… but you can't easily copy&paste code in a console to test it and debug it… which is something that, I promise you, you'll be doing a lot.

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