# Guard

Constrain continuation of computation based on Boolean expression

## Illustration

The notion of guard is pretty general as it occurs in different ways in programming languages which is why we do not attempt a comprehensive description here. Instead, we explain guards in Haskell; they are helpful here for adding more control on pattern matching.

Consider the following function for finding the minimum in a list:

```findmin (x:xs) = findmin' x xs
where
findmin' m [] = m
findmin' m (y:ys) =
if m < y
then findmin' m ys
else findmin' y ys
```

Arguably, it may be preferable to decompose the second case for non-empty list into two cases: one for each branch of the if-then-else. We can guard the equations for the two cases accordingly.

```findmin (x:xs) = findmin' x xs
where
findmin' m [] = m
findmin' m (y:ys) | m < y = findmin' m ys
findmin' m (y:ys) | m >= y = findmin' y ys
```

Arguably, the guard for the last equation is not needed because the equation would only be tried in a case where the condition must evidently hold. So we may want to simplify as follows.

```findmin (x:xs) = findmin' x xs
where
findmin' m [] = m
findmin' m (y:ys) | m < y = findmin' m ys
findmin' m (y:ys) = findmin' y ys
```

Arguably, we do not want to use guards at all, as we may be even more concise (without guards) as follows.

```findmin (x:xs) = findmin' x xs
where
findmin' m [] = m
findmin' m (y:ys) = findmin' (min m y) ys
```

The guards of Haskell provide yet additional expressiveness that we omit here for brevity.

## User contributions

This user never has never made submissions.

## Syntax for editing wiki

For you are available next options:

will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from @worker.

## Syntax for editing wiki

For you are available next options:

will make text bold.

will make text italic.

will make text underlined.

will make text striked.

will allow you to paste code headline into the page.

will allow you to link into the page.

will allow you to paste code with syntax highlight into the page. You will need to define used programming language.

will allow you to paste image into the page.

is list with bullets.

is list with numbers.

will allow your to insert slideshare presentation into the page. You need to copy link to presentation and insert it as parameter in this tag.

will allow your to insert youtube video into the page. You need to copy link to youtube page with video and insert it as parameter in this tag.

will allow your to insert code snippets from @worker.