This Erlang code defines a simple temperature converter module that converts temperatures from Celsius to Fahrenheit. Let's break down the code step by step to understand its functionality, structure, and best practices.
High-Level Overview
The module temp_converter
contains a single exported function, convert_temp/0
, which prompts the user for a temperature in Celsius, performs the conversion to Fahrenheit, and prints the result. It also handles potential input errors gracefully.
Code Breakdown
Module Declaration and Export
-module(temp_converter).
-export([convert_temp/0]).
-module(temp_converter).
: This line declares the module name as temp_converter
. In Erlang, modules are used to group related functions.
-export([convert_temp/0]).
: This line specifies that the function convert_temp/0
(which takes no arguments) can be called from outside the module.
Main Function: convert_temp/0
convert_temp() ->
io:format("Enter temperature in Celsius: "),
Input = io:get_line(""),
try
Celsius = list_to_float(string:trim(Input)),
Fahrenheit = celsius_to_fahrenheit(Celsius),
io:format("~p Celsius is equal to ~p Fahrenheit~n", [Celsius, Fahrenheit])
catch
_:_ ->
io:format("Please enter a valid number.~n")
end.
-
User Input:
io:format("Enter temperature in Celsius: "),
: This line prompts the user to enter a temperature.
Input = io:get_line(""),
: This line reads a line of input from the user.
-
Input Processing:
try ... catch
: This block is used to handle exceptions that may arise during input processing.
Celsius = list_to_float(string:trim(Input)),
:
string:trim(Input)
removes any leading or trailing whitespace from the input.
list_to_float/1
converts the trimmed string to a float. If the input is not a valid number, an exception will be raised.
-
Temperature Conversion:
Fahrenheit = celsius_to_fahrenheit(Celsius),
: This line calls the helper function celsius_to_fahrenheit/1
to convert the Celsius temperature to Fahrenheit.
-
Output:
io:format("~p Celsius is equal to ~p Fahrenheit~n", [Celsius, Fahrenheit])
: This line prints the result, formatting the output to show both the Celsius and Fahrenheit values.
-
Error Handling:
catch _:_ ->
: If an exception occurs (e.g., invalid input), this block catches it and prints an error message.
Helper Function: celsius_to_fahrenheit/1
celsius_to_fahrenheit(Celsius) ->
(Celsius * 9 / 5) + 32.
- This function takes a single argument,
Celsius
, and performs the conversion to Fahrenheit using the formula:
[
\text{Fahrenheit} = \left(\text{Celsius} \times \frac{9}{5}\right) + 32
]
- It returns the calculated Fahrenheit value.
Key Concepts and Best Practices
- Error Handling: The use of
try ... catch
is a good practice for handling potential runtime errors, especially when dealing with user input.
- Modularity: The separation of the conversion logic into a helper function (
celsius_to_fahrenheit/1
) promotes code reusability and clarity.
- User Interaction: The use of
io:format
and io:get_line
allows for interactive command-line input and output, which is user-friendly.
Potential Issues and Areas for Improvement
- Input Validation: While the code handles exceptions, it could be improved by providing more specific feedback on what went wrong (e.g., distinguishing between non-numeric input and other errors).
- Function Naming: The function names are clear, but adding more documentation (comments) could help future developers understand the purpose of each function more quickly.
- Global State: If this module were to be expanded, consider using a state management approach to handle multiple conversions or maintain user preferences.
Alternative Approaches
- Using a GUI: For a more user-friendly experience, consider implementing a graphical user interface (GUI) using a library like
wxErlang
.
- Batch Processing: Instead of a single conversion, the module could be extended to handle multiple temperature conversions in one go, perhaps by accepting a list of temperatures.
This code serves as a solid foundation for a temperature conversion tool in Erlang, demonstrating basic input handling, arithmetic operations, and modular design.