Skip to content

Instantly share code, notes, and snippets.

@mfonism
Last active April 21, 2024 15:17
Show Gist options
  • Save mfonism/019817d2f3651c85cb2ec9675730c262 to your computer and use it in GitHub Desktop.
Save mfonism/019817d2f3651c85cb2ec9675730c262 to your computer and use it in GitHub Desktop.
Elm Homework for Friends at Hamelin

PREV: Types and Values Ninjery!
NEXT: Filter like a real star


Map it like it's hot!

This batch of exercises is designed to enhance your understanding of mapping in Elm. Mapping is a fundamental concept in functional programming that allows you to transform lists by applying a function to each element in the list. These exercises will provide you with practical scenarios to apply this concept, reinforcing your ability to manipulate and process data efficiently in Elm.

Instructions

For each exercise provided, your task is to write a function in Elm that fulfills the requirements specified.

  • Name your functions thoughtfully: Consider what the function does and choose a name that clearly communicates its purpose. Good function names help make your code readable and maintainable.
  • Avoid anonymous functions in your definitions: While Elm supports the use of anonymous (or lambda) functions, for these exercises, please define each transformation function explicitly by name. This practice will aid you in thinking structurally about the functionality of your code and improve the clarity of your logic.

The skills you develop through these exercises will be applicable not just in academic projects but also in real-world software development where data transformation is ubiquitous. By the end of this series, you should feel comfortable using List.map and other related functional programming techniques to handle various data operations in Elm.

Let's get started and dive into the world of functional transformations with Elm!

Exercise 1: Double the Numbers

Task: Write a function that doubles each number in a list of integers.

Example Inputs and Outputs

  • Input: [1, 2, 3]
    Output: [2, 4, 6]
  • Input: [5, 0, -3]
    Output: [10, 0, -6]
  • Input: [100, 50, 25]
    Output: [200, 100, 50]
  • Input: [-1, -2, -3]
    Output: [-2, -4, -6]

Exercise 2: Convert Boolean to String

Task: Convert each boolean in a list to its string representation ("true" or "false").

Example Inputs and Outputs

  • Input: [True, False, True]
    Output: ["true", "false", "true"]
  • Input: [False, False, True, False]
    Output: ["false", "false", "true", "false"]
  • Input: [True]
    Output: ["true"]
  • Input: [False, True, False, True, True]
    Output: ["false", "true", "false", "true", "true"]

Exercise 3: Calculate Areas of Circles

Task: Given a list of circle radii, calculate the area for each corresponding circle. Do you remember the formula for the area of a circle?

Example Inputs and Outputs

  • Input: [1.0, 2.0, 3.0]
    Output: [3.14159, 12.56636, 28.27433]
  • Input: [4.0, 5.0, 6.0]
    Output: [50.26548, 78.53982, 113.09734]
  • Input: [7.0, 8.0, 9.0]
    Output: [153.93804, 201.06193, 254.46900]
  • Input: [10.0, 11.0, 12.0]
    Output: [314.15927, 380.13271, 452.38934]

Exercise 4: Calculate Volumes of Cubes/Cuboids

Task: Given a list of records with dimensions of type Dimensions, calculate the volume for each cuboid.

Type Definition

type alias Dimensions =
    { length : Float
    , height : Float
    , breadth : Float
    }

Example Inputs and Outputs

  • Input: [{length = 1.0, height = 1.0, breadth = 1.0}, {length = 2.0, height = 3.0, breadth = 4.0}]
    Output: [1.0, 24.0]
  • Input: [{length = 5.0, height = 6.0, breadth = 7.0}]
    Output: [210.0]
  • Input: [{length = 8.0, height = 9.0, breadth = 10.0}, {length = 2.5, height = 3.5, breadth = 4.5}]
    Output: [720.0, 39.375]
  • Input: [{length = 7.5, height = 8.5, breadth = 9.5}]
    Output: [721.875]

Exercise 5: Uppercase Strings

Task: Convert all strings in a list to uppercase.

Example Inputs and Outputs

  • Input: ["hello", "world"]
    Output: ["HELLO", "WORLD"]
  • Input: ["good", "morning", "elm"]
    Output: ["GOOD", "MORNING", "ELM"]
  • Input: ["Elm", "is", "Awesome"]
    Output: ["ELM", "IS", "AWESOME"]
  • Input: ["functional", "programming"]
    Output: ["FUNCTIONAL", "PROGRAMMING"]

Exercise 6: Calculate Square Roots

Task: Return the square roots of a list of numbers.

Example Inputs and Outputs

  • Input: [4.0, 16.0, 25.0]
    Output: [2.0, 4.0, 5.0]
  • Input: [1.0, 9.0, 36.0]
    Output: [1.0, 3.0, 6.0]
  • Input: [49.0, 64.0, 100.0]
    Output: [7.0, 8.0, 10.0]
  • Input: [121.0, 144.0, 169.0]
    Output: [11.0, 12.0, 13.0]

Exercise 7: Tag Length

Task: Return the length of each string in a list, along with the original string in a tuple.

Example Inputs and Outputs

  • Input: ["elm", "rocks"]
    Output: [("elm", 3), ("rocks", 5)]
  • Input: ["hello", "world"]
    Output: [("hello", 5), ("world", 5)]
  • Input: ["functional", "programming", "in", "elm"]
    Output: [("functional", 10), ("programming", 11), ("in", 2), ("elm", 3)]
  • Input: ["123", "4567", "89"]
    Output: [("123", 3), ("4567", 4), ("89", 2)]

Exercise 8: Append Index

Task: Append the index of each element to the element itself in a list of strings.

Example Inputs and Outputs

  • Input: ["a", "b", "c"]
    Output: ["a 0", "b 1", "c 2"]
  • Input: ["apple", "banana", "cherry"]
    Output: ["apple 0", "banana 1", "cherry 2"]
  • Input: ["first", "second"]
    Output: ["first 0", "second 1"]
  • Input: ["one", "two", "three", "four"]
    Output: ["one 0", "two 1", "three 2", "four 3"]

Exercise 9: Invert Booleans

Task: Invert each boolean value in a list.

Example Inputs and Outputs

  • Input: [True, False, True]
    Output: [False, True, False]
  • Input: [False, False, True, False]
    Output: [True, True, False, True]
  • Input: [True]
    Output: [False]
  • Input: [False, True, False, True, True]
    Output: [True, False, True, False, False]

Exercise 10: Format Product Display with Currency

Task: Given a list of items, each with an amount and a currency (spelled out as "Dollars", "Pounds", "Euros"), format them into strings that show the currency symbol followed by the amount.

Type Definition

type alias PriceItem =
    { amount : Float
    , currency : String
    }

Example Inputs and Outputs

  • Input: [{amount = 9.99, currency = "Dollars"}, {amount = 7.95, currency = "Pounds"}, {amount = 8.99, currency = "Euros"}]
    Output: ["$9.99", "£7.95", "€8.99"]
  • Input: [{amount = 0.99, currency = "Dollars"}, {amount = 1.50, currency = "Pounds"}, {amount = 2.00, currency = "Euros"}]
    Output: ["$0.99", "£1.50", "€2.00"]
  • Input: [{amount = 25.0, currency = "Dollars"}, {amount = 30.0, currency = "Pounds"}, {amount = 45.0, currency = "Euros"}]
    Output: ["$25.00", "£30.00", "€45.00"]
  • Input: [{amount = 100.0, currency = "Dollars"}, {amount = 100.0, currency = "Pounds"}, {amount = 100.0, currency = "Euros"}]
    Output: ["$100.00", "£100.00", "€100.00"]

Exercise 11: Multiply by Index

Task: Multiply each number in a list by its index.

Example Inputs and Outputs

  • Input: [10, 20, 30]
    Output: [0, 20, 60]
  • Input: [5, 10, 15, 20]
    Output: [0, 10, 30, 60]
  • Input: [3, 6, 9, 12, 15]
    Output: [0, 6, 18, 36, 60]
  • Input: [1, 2, 3, 4, 5, 6]
    Output: [0, 2, 6, 12, 20, 30]

Exercise 12: Generate HTML Tags

Task: Wrap each string in a list with HTML <p> tags.

Example Inputs and Outputs

  • Input: ["Hello", "World"]
    Output: ["<p>Hello</p>", "<p>World</p>"]
  • Input: ["Elm", "is", "awesome"]
    Output: ["<p>Elm</p>", "<p>is</p>", "<p>awesome</p>"]
  • Input: ["Functional", "programming"]
    Output: ["<p>Functional</p>", "<p>programming</p>"]
  • Input: ["Stay", "safe"]
    Output: ["<p>Stay</p>", "<p>safe</p>"]

Exercise 13: Calculate Logarithms

Task: Calculate the natural logarithm of each number in a list.

Example Inputs and Outputs

  • Input: [1.0, Math.e, 10.0]
    Output: [0.0, 1.0, approximately 2.302]
  • Input: [100.0, 1000.0, 10000.0]
    Output: [approximately 4.605, approximately 6.908, approximately 9.210]
  • Input: [2.718281828459, 7.38905609893065, 20.085536923187667]
    Output: [1.0, 2.0, 3.0]
  • Input: [0.5, 0.1, 0.01]
    Output: [approximately -0.693, approximately -2.303, approximately -4.605]

Exercise 14: Replace Empty with Placeholder

Task: Replace empty strings in a list with "Placeholder".

Example Inputs and Outputs

  • Input: ["Elm", "", "Rocks!"]
    Output: ["Elm", "Placeholder", "Rocks!"]
  • Input: ["", "is", "", "awesome"]
    Output: ["Placeholder", "is", "Placeholder", "awesome"]
  • Input: ["Functional", "", "programming", ""]
    Output: ["Functional", "Placeholder", "programming", "Placeholder"]
  • Input: ["", "", "", ""]
    Output: ["Placeholder", "Placeholder", "Placeholder", "Placeholder"]

Exercise 15: Normalize Text

Task: Sanitize strings in a list by trimming whitespace and converting text to lowercase.

Example Inputs and Outputs

  • Input: [" Elm ", " Rocks! "]
    Output: ["elm", "rocks!"]
  • Input: [" FUNCTIONAL ", "programming "]
    Output: ["functional", "programming"]
  • Input: [" Stay Safe ", "stay Healthy"]
    Output: ["stay safe", "stay healthy"]
  • Input: [" LEARN ", " Elm "]
    Output: ["learn", "elm"]

Exercise 16: Find Lengths

Task: Return the length of each string in a list.

Example Inputs and Outputs

  • Input: ["Hello", "World", "Elm"]
    Output: [5, 5, 3]
  • Input: ["Functional", "Programming", "is", "cool"]
    Output: [10, 11, 2, 4]
  • Input: ["12345", "67890", "abc"]
    Output: [5, 5, 3]
  • Input: ["longer phrase", "short"]
    Output: [13, 5]

Exercise 17: Append "day"

Task: Append "day" to the end of each string in a list representing shortened or non-standard days of the week.

Example Inputs and Outputs

  • Input: ["mon", "tue", "wed"]
    Output: ["monday", "tuesday", "wednesday"]
  • Input: ["thu", "fri"]
    Output: ["thursday", "friday"]
  • Input: ["sat", "sun"]
    Output: ["saturday", "sunday"]
  • Input: ["public holi"]
    Output: ["public holiday"]

Exercise 18: Convert Extended String to Boolean

Task: Convert each string in a list to its boolean representation, considering extended variations such as "true", "yes", "y", "1" for True and "false", "no", "n", "0" for False. Case should not affect the conversion.

Example Inputs and Outputs

  • Input: ["true", "no", "Yes", "0"]
    Output: [True, False, True, False]
  • Input: ["FALSE", "y", "N", "1"]
    Output: [False, True, False, True]
  • Input: ["Yes", "yes", "NO", "no"]
    Output: [True, True, False, False]
  • Input: ["T", "f", "t", "F"]
    Output: [True, False, True, False]

Exercise 19: Calculate Future Ages

Task: Write a function that takes a specific year and a list of people (each person having a name, year of birth, and gender), and returns a list showing only the ages each person will be in that year. If a person was not born by that year, their age should be reported as 0.

Example Inputs and Outputs

  • Input: 2025, [{name = "Alice", yearOfBirth = 2010, gender = "Female"}, {name = "Bob", yearOfBirth = 2028, gender = "Male"}]
    Output: [15, 0]
  • Input: 2030, [{name = "Charlie", yearOfBirth = 2025, gender = "Male"}]
    Output: [5]
  • Input: 2050, [{name = "Dana", yearOfBirth = 1990, gender = "Female"}, {name = "Evan", yearOfBirth = 1985, gender = "Male"}]
    Output: [60, 65]
  • Input: 2020, [{name = "Fay", yearOfBirth = 2023, gender = "Female"}, {name = "George", yearOfBirth = 1995, gender = "Male"}]
    Output: [0, 25]

Exercise 20: Calculate Future Ages (version 2)

Task: Write a function that takes a specific year and a list of people (each person having a name, year of birth, and gender), and returns a list showing tuples of each person's name and age they will be in that year. If a person was not born by that year, their age should be reported as 0.

Example Inputs and Outputs

  • Input: 2025, [{name = "Alice", yearOfBirth = 2010, gender = "Female"}, {name = "Bob", yearOfBirth = 2028, gender = "Male"}]
    Output: [("Alice", 15), ("Bob", 0)]
  • Input: 2030, [{name = "Charlie", yearOfBirth = 2025, gender = "Male"}]
    Output: [("Charlie", 5)]
  • Input: 2050, [{name = "Dana", yearOfBirth = 1990, gender = "Female"}, {name = "Evan", yearOfBirth = 1985, gender = "Male"}]
    Output: [("Dana", 60), ("Evan", 65)]
  • Input: 2020, [{name = "Fay", yearOfBirth = 2023, gender = "Female"}, {name = "George", yearOfBirth = 1995, gender = "Male"}]
    Output: [("Fay", 0), ("George", 25)]

PREV: Types and Values Ninjery!
NEXT: Filter like a real star

@Ikechukwunwachukwu
Copy link

thank you for these tasks. rolls up sleeve and gets to work.

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