in this session, we're going to take a look how we can use scalar collections for combinatorial search. In imperative programming when you search for something, you often do that by means of a loop or a series of nested loops. The loops would terminate when an element that matches the condition is found in functional programming. You don't have loops at your disposal, but you do have higher order functions. And it turns out that higher order functions on sequences are very good toolbox to achieve the same objective combinatorial search. And they often need to coat that is clearer and shorter than using loops. For example, let's look at this problem. We are given a positive integer n, we want to find all pairs of positive integers, I and J. Such that j is between one and n exclusive and I is between j and n both exclusive. And a pair of those two inter just should meet the condition that I press J is prime. For example, if any equal seven. Then the pairs of the solution are here in this list, no the smallest set of inter just with a prime. Some is 2 and 1 and the largest is 6 and 5. So how do we find a solution to this problem? A natural way to do so is to generate the sequence of all pairs of integers Ij that are within bounds as a specify. And then filter that sequence for the past for which I plus J is prime. So how do we generate the sequence of pass? Well, we first generate all the integers I between one and n excluded. And then for each integer I given like that we generate a list of past I one and so on. Up to I -1, and that can be achieved by combining until and map so we start with the list one until n. We map it so that for each of those we get the list one until I. And we map that list with a J so that we can get back the pair I and J. So that's close to the solution, but not quite yet. Because like that we get a sequence of sequences. Let's call that sequence of sequences Xs s. So that's a good step towards a solution, but it's not quite what we need yet. Because the previous step gave us a sequence of sequences. Let's call it Xss, what we still have to do is combine all the subsequent asses into one single sequence. And we can do that with fault, right? And plus, plus, so for instance, Xss fall right with the empty sequence and plus plus with the operation. Or equivalently and simpler, we can use the built in method flatten. So we can just write Xss dot flatten. So that gives the together the expression that you see here, we map over one until n. We map over one until I we formed the pairs of I and J, and we flatten the results. He has a useful law for any collection excess, excess flat map F is the same as excess map F, and then flatten. So the above expression can be simplified to one until end flat map. I one until I map J, formed the pair of I and J. So now we need to reassemble the two pieces and we obtain the following expression. One until end flat map I map J, I,J, and then we filter the pairs XY. To keep only those pairs were express y is prime this works. But it makes most people's head hurt, is there a simpler way? Generally higher order functions such as map, flat map or filter, provide powerful constructs for manipulating lists. But sometimes the level of obstruction required by these functions make the program difficult to understand. In this case scalas for expression notation can help and that's what we are going to discover next. We introduce four expressions with an example. That person's be a list of elements of class, person with fields, name and age. So here's the definition of that class. To obtain the names of persons over 20 years old, you can write for p left arrows. Or this was pronounced taken from persons FP dot age greater 20 yield p dot name. This four expression is equivalent to writing person start filter such that p dot h is greater than 20 and then map take the name of the person. So that for expressions looking a bit like a four loops in an imperative language, except that it builds a list of the results instead of working by side effects. So you've seen a particular example of a four expression in general for expressions are of this forum for S yield E. Where S is a sequence of generators and filters and E is an expression whose value is returned at each iteration step. So each E designates one element of the collection that is returned by the for as a whole. So what our generators and filters? Well, the generators of the form P left arrow, E where P is a pattern and is an expression whose value is a collection. And the filter is of the form F, F where F is a boolean expression. The sequence must start with the generator. And if there are several generators in the sequence, the last generators very faster than the first. Here are two examples which were previously solved to tyra functions. The first example was given a positive integer N. Find all pairs of positive vintages with these range conditions such that I plus J is prime. So that can now be written like this. We can write for let's take I from one until end. Let's take J from one until I if is prime, I plus J, then yield I, J as a pair. So this is clearly clearer than the previous example using higher order functions. As a second example, let's write scalar product with the use of a for. So here you have the outline of scalar product. What would it look like if you write it with a forum? So here's the answer. We would have a four expression that still needs to zip Xss and wires. It matches that with the pair XY and for each XY. That comes from this range, it yields X times Y. And finally we have a some of the whole sequence. You might ask the question, well, why couldn't we write it like this for X taken from Xss? Y taken from ys the virus yield X times y would that work as well? Or would it do something different or maybe not compile? The answer is it would compile but it would do something different. It would multiply every element of xs with every element of ys, so not just corresponding elements but every element of xs times, every element of Y. So it would be a quadratic number of terms that we multiply and then summer.