HPM Education - Haskell

Searchβ¦

Introduction

Types in Haskell

Defining Functions / Working with Functions

List Comprehensions

Higher-order Functions

Cutom Types

Interactive Programming

Functors, Applicatives and Monads

Recursion Practice

Let's practice recursion by defining some more recursive functions:

- A function that takes two integers, x and y and raises x to the y power:
- 1.
**Define the type**We know the function will take two integers and return an integer.1power :: Int -> Int -> IntCopied! - 2.
**Enumerate different cases**We can think of two special cases, where either of the arguments is zero, and the general case where both are greater than zero.1power _ 0 =2power 0 _ =3power x y =Copied! - 3.
**Take care of the simple cases first (usually base cases)**Any number to the power of zero is equal to one. Note that we set this pattern firstbecause we want to consider zero to the power of zero to be equal to one. The other simple case is that zero to the power of any other number (other than zero itself) is equal to zero.1power _ 0 = 12power 0 _ = 0Copied! - 4.
**Define the other cases**Now, we get to the recursive case. We can define that`x`

to the power of`y`

is equal to`x`

multiplied by the result of the recursive call to the function with`(y - 1)`

1power x y = x * power x (y -1)Copied!

For practice, write out how this function would be applied step by step.

Let's practice recursion by defining some more recursive functions:

- A function that takes in a list and returns a new list with only the even-indexed elements (with the first element considered odd-indexed):
- 1.
**Define the type**We know the function will take in a list of some type and return a list of the same type.1evens :: [a] -> [a]Copied! - 2.
**Enumerate different cases**Here, we have to think about the cases we can have. The first one is very simple, if the supplied list is empty, we should return an empty list, but we will leave the definitions for what to return for the next step. The next case we can consider is a list with only one element. We are then left with the general case of two or more elements in a list.1evens [] =2evens [_] =3evens (_ : x : xs) =Copied! - 3.
**Take care of the simple cases first (usually base cases)**In the first case (empty list) we already mentioned that we just want to return an empty list. The case with just one element in the list should also return an empty list as that element is odd-indexed and we are only interested in even-indexed elements.1evens [] = []2evens [_] = []Copied! - 4.
**Define the other cases**That last case we laid out is where the list has two or more elements. In this case, we should ignore the first element, take the second one and join it with the result of the recursive call of the remaining list.1evens (_ : x : xs) = x : evens xCopied!

For practice, write out how this function would be applied step by step.

β

Last modified 4mo ago

Copy link