Here are some super easy problems for beginners to practice working with, and begin truly understanding, Haskell’s syntax. These problems will help you get familiar with defining functions, using basic types, and applying Haskell’s fundamental features like recursion, pattern matching, and higher-order functions.
1. Hello, Haskell! 🟢
Write a simple function that greets the user.
Problem: Call greet
with your name as input and “Hello, <name>!” as the resulting string when called.
What You Need to Know:
- Function definitions in Haskell
- Function type signatures
- Basic Haskell syntax
Click for Solution:
-- Type declaration
greet :: String -> String
-- Function
greet name = "Hello, " ++ name ++ "!"
-- Example
ƛ greet Bob
"Hello, Bob!"
2. Add Two Numbers 🟢
Define a function that adds two numbers.
Problem: Call add
with two integers.
What You Need to Know:
- Function definitions in Haskell
- Function type signatures
- Basic arithmetic
Click for Solution:
-- Type declaration
add :: Int -> Int -> Int
-- Function
add x y = x + y
-- Example
ƛ add 8 4
12
3. Double a Number 🟢
Write a function that doubles a given number.
Problem: Call double
with any integer.
What You Need to Know:
- Function definitions in Haskell
- Function type signatures
- Basic arithmetic
Click for Solution:
-- Type declaration
double :: Int -> Int
-- Function
double x = x * 2
-- BONUS: Point-free Function
double = (* 2)
-- Example
double 4
8
4. Check if a Number is Even 🟢
Write a function to check if a number is even.
Problem: Test isEven
with a few numbers.
What You Need to Know:
- Modular arithmetic (
mod
) operator - Boolean values (
True
,False
)
Click for Solution:
-- Type declaration
isEven :: Int -> Bool
-- Function
isEven x = x `mod` 2 == 0
-- Example
ƛ isEven 10
True
5. Find the Length of a List 🟡
Write a function that calculates the length of a list.
Problem: Call listLength
with a few lists like [1, 2, 3]
or ["apple", "banana"]
.
What you need to know:
- Pattern matching
- Recursion
- Lists in Haskell
Click for Solution:
-- Type declaration
listLength :: [a] -> Int
-- Function
listLength :: [a] -> Int
listLength [] = 0
listLength (_:xs) = 1 + listLength xs
-- Example
listLength [1, 2, 3, 4]
4
This Haskell code calculates the length of a list using recursion:
- For a non-empty list (
_:xs
), the length is1
(for the head) plus the length of the tail (xs
). This simple and elegant implementation showcases Haskell’s pattern matching and recursion capabilities. - For an empty list (
[]
), the length is0
.
6. Reverse a List 🟡
Reverse the elements of a list.
Problem: Test reverseList
with a few lists.
What You Need to Know:
Click for Solution:
-- Type declaration
reverseList :: [a] -> [a]
-- Function
reverseList [] = []
reverseList (x:xs) = reverseList xs ++ [x]
-- Example
ƛ reverseList [1, 2, 3, 4]
[4, 3, 2, 1]
The reverseList
function reverses a list by recursively appending the head of the list to the reversed tail. For example, reverseList [1, 2, 3, 4]
returns [4, 3, 2, 1]
.
7. Compute the Factorial of a Number 🟡
Write a recursive function to calculate the factorial of a number.
Problem: Compute factorial 5
or any other integer.
What You Need to Know:
- Recursion
- Pattern matching
- Basic arithmetic
Click for Solution:
-- Type declaration
factorial :: Int -> Int
-- Function
factorial 0 = 1
factorial n = n * factorial (n - 1)
-- Example
factorial 5 -- Calculates: 5 * 4 * 3 * 2 * 1
120
The factorial
function computes the factorial of a number using recursion. For example, factorial 5
returns 120
. 5! = 5 × 4 × 3 × 2 × 1 = 120
8. Double Each Element in a List 🟢
Write a function that doubles each number in a list.
Problem: Test doubleList [1, 2, 3, 4]
.
What You Need to Know:
Click for Solution:
-- Type declaration
doubleList :: [Int] -> [Int]
-- Function
doubleList xs = map (*2) xs
-- Example
ƛ doubleList [1, 2, 3, 4]
[2, 4, 6, 8]
The doubleList
function doubles each element of a list. For example, doubleList [1, 2, 3]
returns [2, 4, 6]
.
9. Filter Odd Numbers from a List 🟢
Filter out all odd numbers from a list.
Problem: Call filterEvens [1, 2, 3, 4, 5, 6]
.
What You Need to Know:
Click for Solution:
-- Type declaration
isEven :: Int -> Bool
-- Function
isEven x = x `mod` 2 == 0
-- Example
ƛ filter isEven [1, 2, 3, 4, 5, 6]
[2, 4, 6]
The filterEvens
function removes odd numbers from a list, leaving only even ones. For example, filterEvens [1, 2, 3, 4, 5, 6]
returns [2, 4, 6]
.
10. Create a Range of Numbers 🟢
Create a list of numbers from 1 to n
.
Problem: Call range 10
and observe the result.
What You Need to Know:
- Ranges (
[a..b]
syntax) - Lists in Haskell
Click for Solution:
-- Type declaration
range :: Int -> [Int]
-- Function
range 0 = []
range n = [1..n]
-- Example
ƛ range 5
[1, 2, 3, 4, 5]
The range
function generates a list of numbers from 1 to a given number. For example, range 5
returns [1, 2, 3, 4, 5]
.
11. Count How Many Times an Element Appears 🟠
Write a function to count occurrences of an element in a list.
Problem: Problem: Test countOccurrences 2 [1, 2, 2, 3, 4, 2]
.
What You Need to Know:
Click for Solution:
-- Type declaration
countOccurrences :: Eq a => a -> [a] -> Int
-- Function
countOccurrences _ [] = 0
countOccurrences y (x:xs) = (if x == y then 1 else 0) + countOccurrences y xs
-- Example
ƛ countOccurrences 3 [1, 3, 2, 3, 4, 3])
3
This countOccurrences
function in Haskell counts how many times a specific element appears in a list using recursion and pattern matching.
It takes two arguments: the element to count and the list to search, and it returns an integer representing the count.
The function handles two cases: for an empty list, it returns 0
, and for a non-empty list, it checks if the head matches the target element, adding 1
if true and 0
otherwise, then recursively processes the tail of the list.
This approach works for any type that supports equality (Eq
), making it versatile for counting occurrences in lists of numbers, characters, or other comparable types.
12. Find the Maximum Element in a List 🟠
Write a function to find the largest number in a list.
Problem: Test maximumInList [3, 1, 4, 1, 5]
What You Need to Know:
Click for Solution:
-- Type declaration
maximumInList :: [Int] -> Int
-- Function
maximumInList [] = error "Cannot find the maximum of an empty list"
maximumInList [x] = x
maximumInList (x:xs) = max x (maximumInList xs)
The maximumInList
function recursively finds the largest number in a list. The line: maximumInList (x:xs) = max x (maximumInList xs)
recursively compares the head of the list (x
) with the maximum of the rest of the list (xs
) until it reaches the base case of a single-element list. It uses the max
function at each step to bubble up the largest value, ultimately returning the maximum of the entire list.
13. Sum the Elements of a List 🟡
Write a function that calculates the sum of a list of integers.
Problem: Test sumList [1, 2, 3, 4]
What You Need to Know:
Click for Solution:
-- Type declaration
sumList :: [Int] -> Int
-- Function
sumList [] = 0
sumList (x:xs) = x + sumList xs
The sumList
function recursively adds the elements of a list. For example, The last line of code recursively sums up the elements of a list. It splits the list into the head (x
) and tail (xs
), adds the head to the sum of the tail (calculated by the recursive call), and continues until the base case ([]
) is reached, where it returns 0
. This effectively calculates the total sum of all the integers in the list.
14. Calculate the Product of a List 🟡
Write a function to compute the product of a list of integers.
Problem: Test productList [1, 2, 3, 4]
What You Need to Know:
Click for Solution:
-- Type declaration
productList :: [Int] -> Int
-- Function
productList [] = 1
productList (x:xs) = x * productList xs
-- Example
ƛ productList [1, 2, 3, 4]
24
The productList
function recursively multiplies all elements of a list. For example, productList [1, 2, 3, 4]
returns 24
. The last line of the function recursively calculates the product of all elements in a list. It splits the list into the head (x
) and tail (xs
), multiplies the head by the product of the tail (calculated by the recursive call), and continues until the base case ([]
), where it returns 1
(it cannot return 0 as it would negate the entire calculation). This computes the total product of the integers in the list.
15. Check if a List is Empty 🟢
Write a function to check whether a given list is empty.
Problem: Test isEmpty [1, 2, 3]
and isEmpty
[ ]
What You Need to Know:
- Pattern matching
- Boolean Values (True/False)
Click for Solution:
-- Type Signature
isEmpty :: [a] -> Bool
-- Function
isEmpty [] = True
isEmpty _ = False
-- Example
ƛ isEmpty [1, 2, 3]
false
16. Check if an Element Exists in a List 🟠
Write a function to determine if an element exists in a list.
Problem: Test elementExists 3 [1, 2, 3, 4]
What You Need to Know:
- Pattern matching
- Recursion
- Equality comparisons (
==
)
Click for Solution:
-- Type declaration
elementExists :: Eq a => a -> [a] -> Bool
-- Function
elementExists _ [] = False
elementExists y (x:xs) = (x == y) || elementExists y xs
-- Example
ƛ elementExists 3 [1, 2, 3, 4]
True
The elementExists
function checks if an element is in a list. The last line of the function recursively checks if an element (y
) exists in a list. It compares the head (x
) with the target (y
) and, if they are equal, returns True
; otherwise, it checks the tail (xs
) by making a recursive call. The base case ([]
) returns False
, meaning the element is not in the list.
The ||
operator in the line (x == y) || elementExists y xs
is the logical OR. It first checks if the current element x
matches the target y
. If this condition is True
, the entire expression immediately evaluates to True
due to short-circuiting, and the function stops further recursion. If the condition is False
, the function proceeds to the recursive call to check the rest of the list. This ensures that the function stops as soon as a match is found, making it more efficient by avoiding unnecessary checks.
17. Find the Last Element in a List 🟡
Write a function to find the last element of a list.
Problem: Test lastElement [1, 2, 3, 4]
What You Need to Know:
Click for Solution:
-- Type declaration
lastElement :: [a] -> a
-- Function
lastElement [] = error "cannot find the last element of an empty list"
lastElement [x] = x
lastElement (_:xs) = lastElement xs
-- Example
ƛ lastElement [1, 2, 3, 4]
4
The lastElement
function recursively finds the last element of a list. The last line of the function recursively traverses the list by ignoring the head (_
) and checking the tail (xs
) until it reaches the base case of a single-element list ([x]
), which it returns as the last element. If the list is empty ([]
), an error is raised.
18. Remove Duplicates from a List 🟠
Write a function that removes duplicate elements from a list.
Problem: Test removeDuplicates [1, 2, 2, 3, 4, 4, 5]
What You Need to Know:
- Pattern matching
- Recursion
- List Filtering
Click for Solution:
-- Type declaration
removeDuplicates :: [Int] -> [Int]
-- Function
removeDuplicates [] = []
removeDuplicates (x:xs) = x : removeDuplicates (filter (/= x) xs)
-- Example
ƛ removeDuplicates [1, 2, 2, 3, 4, 4, 5]
[1, 2, 3, 4, 5]
The removeDuplicates
function removes duplicate elements from a list. The last line removes duplicates from a list by taking the head (x
), filtering out all occurrences of x
from the tail (xs
), and recursively processing the rest of the list. It builds the result by preserving the first occurrence of each element.
19. Repeat a Value N Times 🟠
Write a function that creates a list by repeating a value n
times.
Problem: Test repeatValue 3 'a'
What You Need to Know:
- Pattern matching
- Recursion
- List construction (
:
)
Click for Solution:
-- Type Signature
repeatValue :: Int -> a -> [a]
-- Function Definition
repeatValue 0 _ = []
repeatValue n x = x : repeatValue (n - 1) x
-- Example
ƛ repeatValue 3 'a'
"aaa"
The repeatValue
function creates a list by repeating a value n
times. For example, repeatValue 3 'a'
returns "aaa"
.
20. Drop the First N Elements of a List 🟠
Write a function to remove the first n
elements from a list.
Problem: Test dropN 3 [1, 2, 3, 4, 5]
What You Need to Know:
- Pattern matching
- Recursion
- Base case handling
Click for Solution:
-- Type Signature
dropN :: Int -> [a] -> [a]
-- Function Definition
dropN 0 xs = xs
dropN _ [] = []
dropN n (_:xs) = dropN (n - 1) xs
-- Example
ƛ dropN 3 [1, 2, 3, 4, 5]
[4, 5]
The dropN
function removes the first n
elements of a list.
Conclusion
These problems cover more ground in recursion, pattern matching, and higher-order functions, helping you deepen your understanding of Haskell’s functional programming style.
Leave a Reply