You are here:   ArielOrtiz.com > Programming Languages > Simple Erlang Exercises

Simple Erlang Exercises

Note: This is a class activity, nothing needs to be delivered.

All the following examples assume the code is contained within a module called simple.

  1. Write a function called f2c that takes X degrees Fahrenheit and converts them to degrees Celsius. Examples:
    > simple:f2c(212).
    100.0
    > simple:f2c(32). 
    0.0
    
    Tip: ºC = (ºF - 32) × 5 ÷ 9
  2. Write a function called sign that takes an integer value N. It returns -1 if N is negative, 1 if N is positive greater than zero, or 0 if N is zero. Examples:
    > simple:sign(10).
    1
    > simple:sign(-5).
    -1
    > simple:sign(0).
    0
  3. Write a function called roots that returns a tuple containing the two possible roots that solve a quadratic equation given its three coefficients (A, B, C) using the following formula:

    Quadratic equation formula

    Examples:

    > simple:roots(2, 4, 2).
    {-1.0,-1.0}
    > simple:roots(1, 0, 0).
    {0.0,0.0}
    > simple:roots(4, 5, 1).
    {-0.25,-1.0}
    
    Use the math:sqrt function to compute the square root of a number.
  4. The BMI (body mass index) is used to determine if a person's weight and height proportion is adequate. The BMI may be calculated using the following formula:

    BMI = (Weight) ÷ (Height2)

    Where Weight should be given in kilograms and Height in meters. The following table shows how different BMI ranges are classified:

    BMI range Description
    BMI < 20 underweight
    20 ≤ BMI < 25 normal
    25 ≤ BMI < 30 obese1
    30 ≤ BMI < 40 obese2
    40 ≤ BMI obese3

    Write a function called bmi that takes two arguments: Weight and Height. It should return an atom that represents the corresponding BMI description computed from its input.

    Examples:

    > simple:bmi(55, 1.5).
    normal
    > simple:bmi(45, 1.7).
    underweight
    > simple:bmi(120, 1.6).
    obese3
    
© 1996-2010 by Ariel Ortiz (ariel.ortiz@itesm.mx)
Made with Django | Licensed under Creative Commons | Valid XHTML | Valid CSS