20 Beginner Problems to Solve in Haskell

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

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

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

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)

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:

6. Reverse a List 🟡

Reverse the elements of a list.

Problem: Test reverseList with a few lists.

What You Need to Know:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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:

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.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *