# Solutions to "Scala with Cats": Chapter 7

April 5, 2023

These are my solutions to the exercises of chapter 7 of Scala with Cats.

## Exercise 7.1.2: Reflecting on Folds

If we use foldLeft with an empty list as the accumulator and :: as the binary operator we get back the reversed list:

On the other hand, if we use foldRight with an empty list as the accumulator and :: as the binary operator we get back the same list:

## Exercise 7.1.3: Scaf-fold-ing Other Methods

The following are implementations of the map, flatMap, filter and sum methods for Lists in terms of foldRight:

The sum method makes use of the Numeric type class from the Scala standard library. In the spirit of this book, we could also have created an implementation that uses the Monoid type class instead.

## Exercise 7.2.2.1: Traversing with Vectors

The result of the provided expression is going to be a Vector of Lists, with each being the pairwise combination of the elements from both Vectors:

If we use a list of three parameters, we will get back a Vector of Lists again, but this time each list is going to be of three elements and we will have one list per each possible triple combination of elements from each of the Vectors:

## Exercise 7.2.2.2: Traversing with Options

The return type of the process method is Option[List[Int]] and it will return a Some of the provided input if all integers in the list argument are even and None otherwise. Therefore, it will produce the following for the first call:

And the following for the second call:

## Exercise 7.2.2.3: Traversing with Validated

The provided method will return a Valid with the list argument when all integers of it are even or an Invalid with a List of String for each element that is not even otherwise. Therefore, we get the following for the first call:

And the following for the second call: