Haskell Chapters Practical Tasks
We welcome all developers here! The journey to ultimate fruitfull Plutus requires No Shortcuts! unfortunately. In order to use online GHCi use replit
A: Practical Tasks Instructions

Haskell Chapter 1 Tasks : Introduction

HC1T1 - Task 1: Function Composition

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.

HC1T2 - Task 2: Pure Function Example

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.

HC1T3 - Task 3: Checking if a Number is Greater than 18

Write a function greaterThan18 that checks whether a given number is greater than 18.

HC1T4 - Task 4: Composing a Function to Process Player Data

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.

HC1T5 - Task 5: Laziness in Haskell

Create a function infiniteNumbers that generates an infinite list of numbers. Extract only the first n elements.

HC1T6 - Task 6: Using Type Signatures

Define a function addNumbers that takes two integers and returns their sum.

HC1T7 - Task 7: Converting Fahrenheit to Celsius

Write a function fToC that converts Fahrenheit to Celsius.

HC1T8 - Task 8: Higher-Order Functions

Create a function applyTwice that applies a function twice to an input value.

Finished. Congratulations!

Get your progress token here.

Haskell Chapter 2 Tasks : Data types, Signatures, and Polymorphism

HC2T1 - Task 1: Checking Types in GHCi

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.

HC2T2 - Task 2: Function Type Signatures

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.

HC2T3 - Task 3: Immutable Variables

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.

HC2T4 - Task 4: Converting Between Infix and Prefix Notations

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

HC2T5 - Task 5: Defining and Using Functions

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.

HC2T6 - Task 6: Understanding Int vs Integer

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.

HC2T7 - Task 7: Boolean Expressions

1. Write Boolean expressions that evaluate to:

True using   &&
False using ||
True using   not
A comparison that returns False

Finished. Congratulations!

Get your progress token here.

Haskell Chapter 3 Tasks : Conditions and helper constructions

HC3T1 - Task 1: Check if a number is positive, negative, or zero

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`.

HC3T2 - Task 2: Determine the grade based on a score using guards

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`.

HC3T3 - Task 3: Convert an RGB color to a hex string using let bindings

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)`.

HC3T4 - Task 4: Calculate the area of a triangle using Heron's formula

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`.

HC3T5 - Task 5: Determine the type of a triangle using guards

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`.

HC3T6 - Advanced Task 6: Check leap year using if-then-else

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`.

HC3T7 - Advanced Task 7: Determine the season based on the month using guards

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`.

HC3T8 - Advanced Task 8: Calculate BMI and return category using where

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`.

HC3T9 - Advanced Task 9: Find the maximum of three numbers using let

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`.

HC3T10 - Advanced Task 10: Check if a string is a palindrome using recursion and guards

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"`.

Finished. Congratulations!

Get your progress token here.

Haskell Chapter 4 Tasks : Pattern matching and Case expressions

HC4T1 - Task 1: Define a weatherReport Function

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"

HC4T2 - Task 2: Define a dayType Function

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"

HC4T3 - Task 3: Define a gradeComment Function

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"

HC4T4 - Task 4: Rewrite specialBirthday using Pattern Matching

Rewrite the specialBirthday function using pattern matching instead of if-else statements.

HC4T5 - Task 5: Add a Catch-All Pattern with a Custom Message

Modify specialBirthday to include the age in the return message when it doesn’t match predefined cases.

HC4T6 - Task 6: Identify List Contents Using Pattern Matching

Create a function whatsInsideThisList that returns a string based on the number of elements in a list.

HC4T7 - Task 7: Ignore Elements in a List

Modify firstAndThird to return only the first and third elements of a list, ignoring others.

HC4T8 - Task 8: Extract Values from Tuples

Create a function describeTuple that extracts values from a tuple and returns a string.

Finished. Congratulations!

Get your progress token here.


B: Haskell Editor Compiler and Output
C: Submit Source Code