Define three functions:
1. double: Multiplies a number by 2. 2. increment: Increases a number by 1. 3. doubleThenIncrement: Uses function composition to apply 4. double first and then increment.
Write a function circleArea that calculates the area of a circle given the radius. Ensure that it’s pure and does not depend on any external state.
Write a function greaterThan18 that checks whether a given number is greater than 18.
Write three functions:
1. extractPlayers: Takes a list of tuples (name, score) and extracts the player names. 2. sortByScore: Sorts the list of players by score in descending order. 3. topThree: Returns the top three players.
Compose these functions into getTopThreePlayers.
Create a function infiniteNumbers that generates an infinite list of numbers. Extract only the first n elements.
Define a function addNumbers that takes two integers and returns their sum.
Write a function fToC that converts Fahrenheit to Celsius.
Create a function applyTwice that applies a function twice to an input value.
Get your progress token .
1. Open GHCi and check the types of the following expressions:
1. 42 2. 3.14 3. "Haskell" 4. 'Z' 5. True && False
2. Write down the expected types before checking in GHCi.
1. Write function signatures for the following functions:
1. A function add that takes two Int values and returns their sum. 2. A function isEven that takes an Int and returns a Bool indicating if it's even. 3. A function concatStrings that takes two String values and returns their concatenation.
2. Implement these functions.
1. Define the following immutable variables in Haskell:
1. myAge as an Int 2. piValue as a Double 3. greeting as a String 4. isHaskellFun as a Bool
2. Try modifying one of the variables and observe what happens.
1. Use prefix notation for the following infix expressions:
1. 5 + 3 2. 10 * 4 3. True && False
2. Use infix notation for the following prefix functions:
1. (+) 7 2 2. (*) 6 5 3. (&&) True False
1. Write a function circleArea that takes a Float radius and returns the area of the circle. 2. Write a function maxOfThree that takes three Int values and returns the maximum. 3. Test your functions with different inputs.
1. Define an Int variable smallNumber with the value 2^62. 2. Define an Integer variable bigNumber with the value 2^127. 3. Try to evaluate 2^64 :: Int in GHCi and note the result.
1. Write Boolean expressions that evaluate to:
True using && False using || True using not A comparison that returns False
Get your progress token .
1. Define a function `checkNumber :: Int -> String`. 2. Use an `if-then-else` statement to check if the number is positive, negative, or zero. 3. Return "Positive", "Negative", or "Zero" accordingly. 4. Test your function with `checkNumber 5`, `checkNumber (-3)`, and `checkNumber 0`.
1. Define a function `grade :: Int -> String`. 2. Use guards (`|`) to classify scores into grades: - 90 and above: "A" - 80 to 89: "B" - 70 to 79: "C" - 60 to 69: "D" - Below 60: "F" 3. Test your function with `grade 95`, `grade 72`, and `grade 50`.
1. Define a function `rgbToHex :: (Int, Int, Int) -> String`. 2. Use `let` bindings to format each color component as a two-character hex string. 3. Concatenate the three hex values into a single string. 4. Test your function with `rgbToHex (255, 0, 127)` and `rgbToHex (0, 255, 64)`.
1. Define a function `triangleArea :: Float -> Float -> Float -> Float`. 2. Use `let` to calculate the semi-perimeter `s`. 3. Apply Heron’s formula: `sqrt(s * (s - a) * (s - b) * (s - c))`. 4. Test with `triangleArea 3 4 5` and `triangleArea 7 8 9`.
1. Define a function `triangleType :: Float -> Float -> Float -> String`. 2. Use guards to classify the triangle: - All sides equal: "Equilateral" - Two sides equal: "Isosceles" - No sides equal: "Scalene" 3. Test with `triangleType 3 3 3`, `triangleType 5 5 8`, and `triangleType 6 7 8`.
1. Define `isLeapYear :: Int -> Bool`. 2. Use `if-then-else` to check: - Divisible by 400: True - Divisible by 100 but not 400: False - Divisible by 4: True - Otherwise: False 3. Test with `isLeapYear 2000`, `isLeapYear 1900`, and `isLeapYear 2024`.
1. Define `season :: Int -> String`. 2. Use guards to return: - 12, 1, 2 -> "Winter" - 3, 4, 5 -> "Spring" - 6, 7, 8 -> "Summer" - 9, 10, 11 -> "Autumn" 3. Test with `season 3`, `season 7`, and `season 11`.
1. Define `bmiCategory :: Float -> Float -> String`. 2. Use `where` to calculate BMI: `bmi = weight / height^2`. 3. Use guards to classify BMI: - <18.5: "Underweight" - 18.5 to 24.9: "Normal" - 25 to 29.9: "Overweight" - >=30: "Obese" 4. Test with `bmiCategory 70 1.75` and `bmiCategory 90 1.8`.
1. Define `maxOfThree :: Int -> Int -> Int -> Int`. 2. Use `let` to store intermediate max values. 3. Return the maximum of the three numbers. 4. Test with `maxOfThree 10 20 15` and `maxOfThree 5 25 10`.
1. Define `isPalindrome :: String -> Bool`. 2. Use guards: - If the string has 0 or 1 characters: True - If the first and last characters match, recursively check the rest. - Otherwise, return False. 3. Test with `isPalindrome "racecar"`, `isPalindrome "haskell"`, and `isPalindrome "madam"`.
Get your progress token .
Create a function weatherReport :: String -> String that takes a weather condition and returns a message describing the weather.
"sunny" → "It's a bright and beautiful day!" "rainy" → "Don't forget your umbrella!" "cloudy" → "A bit gloomy, but no rain yet!" Any other input → "Weather unknown"
Create a function dayType :: String -> String to determine if a day is a weekday or weekend.
"Saturday", "Sunday" → "It's a weekend!" Any other valid day → "It's a weekday." Invalid input → "Invalid day"
Write a function gradeComment :: Int -> String that provides feedback based on a numerical grade.
90 - 100 → "Excellent!" 70 - 89 → "Good job!" 50 - 69 → "You passed." 0 - 49 → "Better luck next time." Any other number → "Invalid grade"
Rewrite the specialBirthday function using pattern matching instead of if-else statements.
Modify specialBirthday to include the age in the return message when it doesn’t match predefined cases.
Create a function whatsInsideThisList that returns a string based on the number of elements in a list.
Modify firstAndThird to return only the first and third elements of a list, ignoring others.
Create a function describeTuple that extracts values from a tuple and returns a string.
Get your progress token .