PREV: Types and Values Ninjery!
NEXT: Filter like a real star
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.
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!
Task: Write a function that doubles each number in a list of integers.
- 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]
Task: Convert each boolean in a list to its string representation ("true" or "false").
- 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"]
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?
- 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]
Task: Given a list of records with dimensions of type Dimensions
, calculate the volume for each cuboid.
type alias Dimensions =
{ length : Float
, height : Float
, breadth : Float
}
- 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]
Task: Convert all strings in a list to uppercase.
- 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"]
Task: Return the square roots of a list of numbers.
- 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]
Task: Return the length of each string in a list, along with the original string in a tuple.
- 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)]
Task: Append the index of each element to the element itself in a list of strings.
- 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"]
Task: Invert each boolean value in a list.
- 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]
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 alias PriceItem =
{ amount : Float
, currency : String
}
- 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"]
Task: Multiply each number in a list by its index.
- 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]
Task: Wrap each string in a list with HTML <p>
tags.
- 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>"]
Task: Calculate the natural logarithm of each number in a list.
- 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]
Task: Replace empty strings in a list with "Placeholder".
- 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"]
Task: Sanitize strings in a list by trimming whitespace and converting text to lowercase.
- 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"]
Task: Return the length of each string in a list.
- 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]
Task: Append "day" to the end of each string in a list representing shortened or non-standard days of the week.
- 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"]
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.
- 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]
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.
- 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]
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.
- 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
thank you for these tasks.
rolls up sleeve and gets to work.