Skip to main content

Day 1 - Numbers

Skills: None

Pre-reading: 3.1.1 to 3.1.4

Intro (10 mins)

Goal Describe what course is about, show Pyret environment.

This course is called "Introduction to Program Design and Implementation".

  • Can anyone tell me what they think a program is?
    • written constructs, that have precise meaning -- instructions of how to do something, carry out operations, etc.
  • What does the work Implementation here mean?
    • once you know what a program should do, how it should be organized, actually writing it down requires understanding precise rules. And often, the first time you write it down, it isn't right -- implementation also covers the idea of validating it, making sure it actually behaves as you expect.
  • What does the work Design here mean?
    • the languages we used to write programs allow us to very quickly make programs that are hard to understand, possibly even by the person that wrote them. We use Design here to stand for skills to organize our programs so that they are easy to write, easy to understand, and easy to modify -- and this ends up being much more important (and thus, occurs first) than whether they "work" (the Implementation part), given "working" is usually only a temporary state, until requirements for the program change.

What is Pyret?

Pyret is a programming language designed explicitly for computer science education, and just like designing our programs results in better programs, designing our tools can help get better results. In particular, Pyret allows us to teach you more than we would be able to otherwise. It also provides a smooth transition, in the latter part of the course, to programming with Python, an industrial language that is now the most widely used language in the world.

How do we use Pyret?

TODO: add url-share url to CPO. These notes will be public, so students can click them. NOTE that in the future, they will use a scratch GH repository, but not going to create GH accounts / repos today.

All editing will happen in the browser.

Show Definitions vs Interations Today we are going to focus on the interactions window. We can write arithmetic expressions -- i.e., 2 + 3, 5 * 4, 18 / 3. These evaluate to values -- 5, 20, 6. Values are results, expressions are bits of code that can run to produce values.

  • Note that Pyret requires spaces around operators: 2 + 3 not 2+3.
  • Also, Pyret requires use of parenthesis to disambiguate operators (no PEMDAS!). (2 + 3) * 4 vs. 2 + (3 * 4). Note what happens if you write 2 + 3 * 4.

Class Exercise (40 mins)

  • Calculate the area of a rectangle with length 10 and width 3.
  • Calculate its perimeter.
  • Note that many different expressions can produce the same value. Come up with three different expressions that all evaluate to the value 10.
  • Read the beginning of https://pyret.org/docs/latest/numbers.html, on ExactNums vs RoughNums, and then look at some of the functions below -- unlike operators, a function, e.g., num-max, when applied to two numbers, is written num-max(10,20). Come up with an expression that uses only ExactNums, but produces a RoughNum when evaluated.

Wrap-up (10 mins)