### 2` `Basic Data and Expressions

Let’s think about some of the programs we use. We might use Google, a search engine: it consumes a search query and produces a search results. We may use Facebook, a social network: it consumes information about our friendships and produces updates from our friends. We could use Amazon, a store: it consumes descriptions of things we’re interested in and produces lists of products that match our descriptions. We sometimes use Weather.com, a weather site: it consumes our location and produces a weather forecast for that location. In fact these systems consume and produce even more: they consume our history of past preferences, and produce ads, recommendations for related products [REF collaborative filtering], and so on. In short, programs consume and produce information.

41.8236 N, 71.4222 W |

+41.8236, -71.4222 |

Therefore, to write programs, we have to understand their data. We will begin by understand the most basic kinds of data, and writing some simple programs with them. As our programs get more sophisticated, we will need to represent more interesting kinds of information.Much of our presentation is directly derived from the book How to Design Programs, which you can find at http://htdp.org/.

#### 2.1` `Numbers

1764

1729

> 1764 |

1764 |

> 1729 |

1729 |

> 1764 + 1729 |

3493 |

> 1764 - 1729 |

35 |

> 1764 * 1729 |

3049956 |

> 1764 * 1729 * 1764 * 1729 * 1764 * 1729 * 1764 * 1729 |

86531512776056800758948096 |

Do Now!

The examples use only one operator per expression. What happens if we try to combine multiple different operators?

3 - 2 + 1

> num-sqrt(4) |

2 |

> num-expt(2, 4) |

16 |

Exercise

A natural question you might have is, “Do I need to put spaces after the commas between parameters?” Good question! Go ahead and check for yourself.

(1 / 3) * 3

(1764 * 1729 * 1764) / (1764 * 1729 * 1764 * 1729)

Exercise

What happens if you try to divide by zero?

What do you expect?

What does Pyret do?

#### 2.2` `Expressions Versus Values

Before we proceed, we have to introduce one piece of terminology. A value is the result of an expression: we are done computing with it and can’t do any more. Thus, 1764 * 1729 is not a value, because we can still perform the multiplication; but 3049956 is a value because it has no more operations waiting to be done.

50 * 1/6

150 * 1/6

Do Now!

Do you see something curious in the fractions we’ve written in the previous section and this one?

This is subtle, so don’t worry if you didn’t catch it: we wrote 1/1729 instead of 1 / 1729, and 1/6 instead of 1 / 6. Weren’t all arithmetic operations (like /) supposed to be surrounded by spaces?

That’s because 1/1729 is not an expression, it’s a
value—

#### 2.3` `Variables to Name Values

brown-founding = 1764 this-year = 2016

this-year - brown-founding

brown-age = this-year - brown-founding

#### 2.4` `Strings

"Providence, RI" "Bangalore, India" "the quick brown fox jumped over the lazy dog"

'Providence, RI' 'Bangalore, India' 'the quick brown fox jumped over the lazy dog'

"The book was called "Structure and Interpretation"" |

Do Now!

What happens when you enter this?

'The book was called "Structure and Interpretation"'

"The book was called \"Structure and Interpretation\""

##### 2.4.1` `Multi-Line Strings

"Let us go then, you and I, |

When the evening is spread out against the sky" |

Do Now!

Try this out and see the resulting error. Become familiar with it!

```Let us go then, you and I, When the evening is spread out against the sky```

"Let us go then, you and I,\nWhen the evening is spread out against the sky"

##### 2.4.2` `Operations on Strings

> "will." + "i." + "am" |

"will.i.am" |

> "The Lovesong " + "of J. Alfred Prufrock"Observe the space at the end of the first string. |

"The Lovesong of J. Alfred Prufrock" |

> s = "The Lovesong" |

> string-length(s) |

12 |

> string-substring(s, 0, 3) |

"The" |

#### 2.5` `Booleans

We have now multiple times talked about two values being equivalent. Right now we’re trusting our eyes to tell that they’re equal, but we can do better: we can ask Pyret to check for us.

Before we do so, we should ask ourselves how Pyret can report back what it finds. For instance, if we ask whether two strings are equal, the answer might be yes and it might be no. What data should Pyret use to indicate the answer?

Exercise

Explain why numbers and strings are not good ways to express the answer.

> 1 == 1 |

true |

> 1 == 2 |

false |

n1 = (1764 * 1729 * 1764) / (1764 * 1729 * 1764 * 1729) n2 = 1/1729 n1 == n2

s1 = ```Let us go then, you and I, When the evening is spread out against the sky``` s2 = "Let us go then, you and I,\nWhen the evening is spread out against the sky" s1 == s2

```Let us go then, you and I, When the evening is spread out against the sky``` == "Let us go then, you and I, When the evening is spread out against the sky"

In particular, we are using the language to determine when things are equal, rather than using our (perhaps wobbly) eyesight. This is a really important idea, which we will return to in much more detail later [REF testing].

##### 2.5.1` `Other Comparisons

> 1 <= 2 |

true |

> 1 > 3 |

false |

> "a" < "b" |

true |

> "a" >= "c" |

false |

> "that" < "this" |

true |

> "alpha" < "beta" |

true |

> "a" >= "C" |

true |

> "a" >= "A" |

true |

Exercise

Can you compare true and false? Try comparing them for equality, then for inequality (such as <).

> "a" == 1 |

false |

> num-equal(1, 1) |

true |

> num-equal(1, 2) |

false |

> string-equal("a", "a") |

true |

> string-equal("a", "b") |

false |

Exercise

Try

num-equal("a", 1)

string-equal("a", 1)

and understand how these operators relate to ==.

##### 2.5.2` `Other Boolean-Producing Operations

> wm = "will.i.am" |

> string-contains(wm, "will") |

true |

> string-contains(wm, "Will")Note the capital W. |

false |

##### 2.5.3` `Combining Booleans

Often, we want to base decisions on more than one Boolean value. For
instance, you are allowed to vote if you’re a citizen of a country
and you are above a certain age. You’re allowed to board a bus
if you have a ticket or the bus is having a free-ride day. We
can even combine conditions: you’re allowed to drive if you are above
a certain age and have good eyesight and—

> (1 < 2) and (2 < 3) |

true |

> (1 < 2) and (3 < 2) |

false |

> (1 < 2) or (2 < 3) |

true |

> (3 < 2) or (1 < 2) |

true |

> not(1 < 2) |

false |

##### 2.5.4` `Using Booleans

One way to use Boolean values is to combine them to produce other Boolean values. Ultimately, however, we’d like to produce other kinds of data as well depending on a Boolean. There are several ways to do this in Pyret, but for now we’ll focus on just one: the conditional expression (because what value it produces is a “condition” of other values).

if <Boolean expression to check>: <expression if true> else: <expression if false> end

brown-founding = 1764 rice-founding = 1912 if brown-founding < rice-founding: "Brown is older" else: "Rice is older" end

if <Boolean expression to check>: <expression if first expression is true> else if <another Boolean expression to check>: <expression if first expression is false and second expression is true> else: <expression if both expressions are false> end

if brown-founding < rice-founding: "Brown is older" else if brown-founding == rice-founding: "Both are the same age" else: "Rice is older" end

#### 2.6` `Evaluating by Reducing Expressions

hours = 45

if hours <= 40: hours * 10 else if hours > 40: (40 * 10) + ((hours - 40) * 15) end

if 45 <= 40: 45 * 10 else if 45 > 40: (40 * 10) + ((45 - 40) * 15) end

=> if false: 45 * 10 else if 45 > 40: (40 * 10) + ((45 - 40) * 15) end

=> if false: 45 * 10 else if true: (40 * 10) + ((45 - 40) * 15) end

=> (40 * 10) + ((45 - 40) * 15)

=> 400 + (5 * 15) => 475

This style of reduction is the best way to think about the evaluation of Pyret expressions (and later, functions). The whole expression takes steps that simplify it, proceeding by simple rules. You can use this style yourself if you want to try and work through the evaluation of a Pyret program by hand (or in your head).

#### 2.7` `Images

include image

red-circ = circle(60, "solid", "red")

white-rect = rectangle(300, 200, "solid", "white")

overlay(red-circ, white-rect)

sm-circ = circle(50, "outline", "black") above(sm-circ, above(sm-circ, sm-circ))

unit = 100 bg-width = unit * 3 bg-height = unit * 2 circ-rad = 3/5 * 1/2 * bg-height red-circ = circle(circ-rad, "solid", "red") white-rect = rectangle(bg-width, bg-height, "solid", "white") overlay(red-circ, white-rect)

Of course, each time we have to keep running the program to compute these values afresh, a problem we’d like to avoid; we’ll return to this later [FILL]. Later we will also see how we can combine images with other computation to make movies, animations, and videogames [REF world].

#### 2.8` `Roughnums

Before we conclude looking at basic data, we have to say a little more about numbers. The numeric examples we’ve picked above have been chosen conveniently to not reveal a certain ugliness about how computers treat numbers. Though Pyret masks this ugliness for the most part, for various reasons (mainly having to do with efficiency), it does not mask them entirely.

~1.4142135623730951

> ~1 + 1 ~2

> num-sqrt(2) - num-sqrt(2) ~0

> num-sqrt(2) + 1 ~2.414213562373095

~1.4142135623730951 ~2.414213562373095

> (num-sqrt(2) + 1) - 1 ~1.414213562373095

> ((num-sqrt(2) + 1) - 1) - num-sqrt(2) ~-2.220446049250313e-16

Exercise

Do roughnums obey the axioms of arithmetic operators like (where appropriate) distributivity, associativity, and commutativity?

1 == ~1

A full explanation of floating points is well beyond the scope of this document; this section just warns you of what you must beware.The interested reader should find a copy of “What Every Computer Scientist Should Know About Floating-Point Arithmetic” by David Goldberg, e.g., see here.