Using Booleans (with some Dyalog APL examples)

Sam GutsellAPL, Computer ScienceLeave a Comment

Booleans are one of the most prominent data types used throughout computing. In programming, Booleans are mainly used in logic problems and can be used and manipulated in many ways.

What is a Boolean?

A Boolean is a data type used to identify a value which can only be true or false, sometimes represented as 1 or 0, respectively. The Boolean data type is named after George Boole, an English mathematician and logician. Boole’s work on algebraic and logical systems is the basis of all modern computer systems.

So now we know what a Boolean is, how are they used in everyday life?

Real world use cases

Before we start looking at code examples, I wanted to demonstrate how Boolean logic is applied in everyday life, so let’s look at a couple of examples.

Example 1: On a most electronic household devices you would use a switch/button to control the on/off state of the device, this state is representative of a Boolean value as the device can only be on or off (True or false, 1 or 0).

Example 2: I’m about to make a cup of tea and I check to see if there is water in the kettle, this is either true or false. Let’s say there is no water in the kettle, then I should fill it up. This is an example of Boolean logic, I’m deciding to carry out an action based on something being either true or false.

So how do we evaluate these values in APL?

Writing Boolean logic and evaluations

Taking a simple true or false like in example 2 above, we can use this to write an instruction based on this Boolean value:

If the kettle is empty
		Fill it up

This is a fairly simple evaluation, and is easy to mimic in APL:

:If IsKettleEmpty

Where IsKettleEmpty is a Boolean variable indicating if the kettle is empty and FillKettle is a method to fill the kettle.

We can also evaluate multiple conditions at the same time using logical operators, which even though it may sound robotic, it is also something we all do on a regular basis. For example, when deciding to leave the house, I can’t do so unless I have shoes on and the door is unlocked. This is using two Boolean values to determine when an action can be carried out.

A slightly less simple evaluation but still not too difficult, let’s write an instruction:

If the door is unlocked and my shoes are on
		Leave the house

Again, let’s mimic this in APL:

:If DoorUnlocked∧ShoesOn

Where DoorUnlocked is a Boolean variable indicating if the door is unlocked, ShoesOn is a Boolean variable that indicates if my shoes are on, and LeaveHouse is a method for me to leave the house.

You will have noticed that there is a symbol (∧) in the above APL code, this symbol represents an “and”, so when we say “the door is unlocked and my shoes are on” the “and” is a logical operator. This allows us to combine two Booleans so we can meet specific circumstances before we carry out an action.

Logical operators

We have seen an example of the logical operator “and” (∧) however, there are some others. We also have “or” and “not”, these allow us to better specify other circumstances. When applying a “not” to a Boolean value it is effectively working the same way it does in English, if you say it is not raining then you are indicating that the state of raining is currently false, if a value is 0 and we apply a “not” then it would return a 1. The way we represent “not” in APL is by using ~, this means that if we wanted to represent “not” raining in APL the same way we have in English, we can write:


Unlike the “not” logical operator, “or” does not work the same as it does in English, this is because we use the word "or" to offer up a choice like chocolate or strawberry, however, in computing we use it to say if one of these values is true then the whole statement is true. Let’s say I’m looking to purchase a car and I’m only interested in red “or” blue cars. We would need to apply a filter to all the cars on offer, this will then evaluate each car’s colour and only show me the red “or” blue ones.

The instruction would look like this:

If the car’s colour is blue or red
      Show the car

Let's write that in APL:

:If (CarColour≡’Red’)∨CarColour≡’Blue’

Where CarColour is the text value of the colour of the car i.e ‘Red’, and ShowCar is a method to show me the car.

You may have noticed we are using two symbols here, one is for evaluation purposes and the other is the symbol for “or”. The symbol for “or” is ∨, however, the other symbol (≡), is “match”, this allows to check if CarColour is either ‘Red’ “or” ‘Blue’, the result of CarColour ”match” ‘Red’ will either be 1 or 0 (True or False). Now, it must be said that there are easier ways to write this in APL, but I have written it this way to make it easier to demonstrate.

Truth tables

When dealing with Boolean logic it can become quite complex at times, with multiple conditions and evaluations in the same expression. One method of attempting to understand, and sometimes simplify Boolean logic is the use of truth tables. These allow you to map out and visualise the logic being used, let’s use one of our examples to demonstrate.

Let’s use my condition for when I can leave the house, which was DoorUnlocked “and” ShoesOn. The way we would represent this in a truth table is by listing out all possible combined states for our variables, using a column for each individual variable and a column for the result of the full expression. Normally, the column headers for the variables would just be represented by letters, however, I have included the names for clarity.

A (DoorUnlocked)B (ShoesOn)A∧B

As you can see in the third column, the whole expression is only true when both A “and” B are true.

If we take this structure and use an "or" instead, the truth table for “or” looks like this:


Now, if we add another logical operator to this expression, in “not” (~), and apply it to B, the truth table would look like this:


You will have noticed that I added a column for ~B, adding columns for parts of the expression is something that is sometimes done to try and break the expression down into more manageable parts.

What real world examples can you find?

There is lot more to learn about Booleans, a lot more than I have covered. So, if this has piqued your interest, I’d suggest going out and trying to learn more about Booleans. I’d really like to hear about any other real world examples you can think of or can find, so please leave a comment if you have any.

About the Author

A picture of Sam Gutsell, the author of this blog

Sam Gutsell

APL Developer

Sam is an APL Developer at Optima Systems. He started at Optima in 2012 as an apprentice straight after finishing College. Sam decided to take Computing whilst studying at college, yet, he never intended to but, he thought it sounded interesting at the time. Sam’s original career plans involved studying Meteorology at university, to become a Meteorologist. His plans changed when he realised how much he enjoyed code. More about Sam.

More from Sam

Other Posts
  • Related
  • Related