Lecture 1 Slides - Programming in Text (In-Class Slides)

1 of 44

Lecture 1

Programming in Text (Mini-Quiz 1)

2 of 44

How to login to PollEverywhere!

On your phone / tablet, either scan the QR code here (or up on the screen) or go to pollev.com/baincs111

Step 1. CLICK LOGIN

Step 2. USE @U.NORTHWESTERN EMAIL ADDRESS

Step 3. CLICK LOGIN WITH NORTHWESTERN

Step 4. Follow sign-in steps with MFA

Step 5. You may need to go back to pollev.com/baincs111 after you've setup your account. Make sure to set your NetID as your Screen Name!!!

3 of 44

Reminders

  • Register for edSTEM! (details on the edSTEM page on Canvas…try searching!)
  • You're responsible for any content in the pre-recorded lectures.
    • Tutorials will DEPEND on the content of those pre-recorded lectures
  • Make sure to READ THE SYLLABUS

  • If you forgot to watch the pre-recorded lecture and/or take MQ0, you can do so up until midnight tonight.
  • Everyone who participates fully in MQ 1 (our first in-class MQ) will see their credit posted this afternoon (~5pm) (i'll post on edSTEM when they get uploaded)
  • Exercise 0 is due TONIGHT – we'll do the programming portion in class, but the survey portion needs to be done tonight as well!

4 of 44

Big ideas from Data Flow Recorded Lecture

  • Data consists of objects
  • Objects come in different types
    • Determines what operations can be performed on it
  • Inputs and outputs of functions are objects
  • Most computation is done by chaining function calls

5 of 44

Review 0. (not a question)

6 of 44

Review 1.

7 of 44

Review 2.

8 of 44

exceptions

9 of 44

What’s the output of this diagram?

10 of 44

Exceptions

  • Functions fail if there’s something wrong with their inputs
  • They “signal an exceptional condition”
    • Or more commonly: “throw an exception
  • This typically stops the program
    • And places you in a debugger program to let you find out what’s happening
  • In this case, the problem is that there are too few arguments (inputs), so we get an arity mismatch exception (arity means the number of inputs)

Wrong number of inputs

11 of 44

What about this one?

12 of 44

What about this one?

concat needs two strings!

  • The most common kind of problem with a function call is sending the wrong type (kind) of input to a function
  • Each input needs to have the right type of data
  • Sending something the wrong kind of input will produce an argument type exception

13 of 44

What about this one?

14 of 44

What about this one?

  • One of the reasons type mistakes are so common is that it’s easy for other kinds of mistakes to trigger them

  • In this case, we switched the order of the inputs to prefix
    • Prefix really wants the first argument to be a string, not a number
    • And it isn’t smart enough to realize it just needs to swap the two arguments

Argument type exception

15 of 44

Type signatures

16 of 44

Types and type checking

  • One nice thing about type errors is that a lot of them can be detected before the program is even run
  • This is done by a kind of metaprogram called a (static) type checker

Argument type

exception

17 of 44

Type signatures

  • Consequently, it’s important to keep track of what types of inputs and outputs are required by each function
    • Known as its type signature
  • You should always think about the type signature of a function when you use it
    • And whether the types of the inputs match up

18 of 44

Types and type checking

  • In this case, a static checker can tell this program will run astray without even knowing the inputs
    • prefix has to have a string as its first input
    • subtraction always generates a number as its output

19 of 44

Programming in Text

  • Diagrams are good for making the flow of data explicit in programs
    • But they can be cumbersome for large programs
    • And there are some techniques they don’t express well
  • So the vast majority of programming languages are text-based
  • We’ll be seeing the basics of ISL+ today

20 of 44

The�language question

21 of 44

What’s the best language?

  • There isn’t one
    • Different languages for different projects
    • Languages come and go pretty fast
    • “We use about 10 languages a year, but they change from year to year
  • You want to learn to program
  • You want to learn programming paradigms/styles
    • Functional
    • Imperative
    • Object-oriented
    • We’ll do all three of these this quarter
  • You want to learn to learn languages
    • No matter what we teach you, 80% of you won’t be using at your jobs

22 of 44

The Racket language

We'll start the quarter using Racket’s Intermediate Student Language

  • Low barrier to entry (for those with and without programming experience)
  • More powerful than more conventional languages
  • Specifically designed to use to learn how to program
    • Racket is a programming language that can be used to design other programming languages.
    • Multi-paradigmatic – we'll actually change the features we have in the language as we learn more over the course of the quarter

23 of 44

This is ISL+ (intermediate student language)

  • Constants
    • 4 means the number 4
    • "I am some text" means a text string that says “I am some text”
  • Variables
    • x means “give me the value of the variable named x
  • Definitions
    • (define name value) means “make a variable called name with that value
  • Conditionals
    • (if condition then else) means then if condition is true, otherwise else
  • Create a Function
    • (λ (x y) z) means “make me a function that takes x and y as inputs and returns z as the output”
  • Call a Function
    • Otherwise (a b c) means “run a with b and c as inputs”

24 of 44

COMP_SCI 111 Lore

  • The syntax of "Racket" is hard honestly, it's hard to learn syntax in any language
  • Recursion
    • That’s not a 111 thing, that’s a programming technique
  • Functional programming, higher-order functions, λ expressions
    • Again, these are programming techniques
    • C++, Python, Java have them too
      • Example: C#’s database query system is ripped off from Scheme/Racket
    • You NEED to know them to show that you are a flexible thinker

25 of 44

26 of 44

Basics of ISL+

Functionality

  1. Names
    1. Constants
    2. Variables
  2. Function Calls
    • Basic Function Calls
    • Chained Function Calls

Functional Aesthetics

  1. Legibility

Turning DFD into Text Programs

27 of 44

Let's figure out how to turn this into a text program

28 of 44

Names

  • Any language (ISL+, C++, English) has to provide some mechanism for naming objects
  • In programming languages, those things are generally data objects
    • Numbers
    • Functions
    • Strings
    • Etc.
  • Everything in ISL+ has a name

29 of 44

Name Type 1: Constants

  • Names whose spelling determines the object being referred to
    • Programmer can’t change their meaning
  • The most basic kind of name

Examples (we'll see more types later)

  • Numbers
    • Sequence of digits means a number
    • Can also include decimal point and/or sign
    • Examples: 1, 7, -2.5
  • Strings
    • Any text enclosed in "" (double quotes) names a text string
    • Ex: "this is a string"

30 of 44

Name Type 2: Variables

  • Arbitrary names that the programmer can use to denote anything they want
  • Any sequence of letters, numbers, and most punctuation marks, that doesn’t look like a number
      • A, b, c, test, blah, foo, x, x1
      • +, -, *, /
      • this-is-a-variable-name
      • as-is-this
  • Variables are like a named container in which you store objects
  • Case-sensitive: x is different from X
  • +1 is a number, 1+ is a variable name
  • Many variables come predefined:
    • string-append, +, -, *
  • We also can define our own variables*
  • Can refer to different objects at different times*

31 of 44

So that means…

32 of 44

So that means…

constant (number)

constant (number)

variable

(name: +)

(value: the plus function)

33 of 44

How do we call or execute a function?

constant (number)

constant (number)

variable

(name: +)

(value: the plus function)

34 of 44

The call function

  • Static checkers are too complicated to use as an example
  • So let’s use a simpler example: a function that calls other functions
    • Inputs: the function and its arguments
    • Output: the output of the call to the input function

35 of 44

Function calls

  • To call a function, write the function, followed by its inputs, separates by spaces:

function input1 … inputn

  • Then group them with parentheses:

(function input1 … inputn)

  • Line breaks and other extra whitespace aren't "code" in Racket

Examples:

  • (+ 1 2)
  • (+ 1 2 3)
  • (string-append "1 +" "2")
  • (string-append "1" "+" "2")

Note: + and string-append can allow variable numbers of inputs in Racket (i.e. they are variadic)

36 of 44

Equivalent diagrams

(+ 1 2) means:

or really:

37 of 44

Exercise 0! Getting setup with DrRacket and writing a program.

38 of 44

extra

exercises

39 of 44

Skills to build

Look at a data flow diagram and:

  • Understand what the actual type of each object is
  • Compare them to the type signatures of the functions
  • Recognize when a function has
    • An input is of the wrong type
    • The wrong number of inputs
    • Inputs in the wrong order

40 of 44

Which of these has problems?

41 of 44

Which of these has problems?

+

1

2

+

4

1

+

2

+

2

4

call

1

+

1

call

1

2

3

+

Bad function

(1 isn’t a function)

42 of 44

Which of these has problems?

43 of 44

Which of these has problems?

call

1

2

+

call

call

+

2

1

call

1

2

+

call

4

+

“woof woof”

prefix

+

5

length

Bad function

(3 isn’t a function)

3

9

14

Index out of range

(string doesn’t have 14 characters)

Bad function

(1 isn’t a function)

44 of 44

Next week on CS 111…

  • Today
    • MQ0 and Exercise 0 due at 11:59pm
      • MQ0 grade posts immediately when you take the quiz
      • MQ1 grades will post by 5pm (unless you weren't in class – then you should take the Canvas version)
    • MAKE SURE TO FILL OUT THE SURVEY
    • If you want to pre-form a group, make sure to do so on Canvas by Sunday at 5pm
    • I have OH today in this room at 3pm
  • Monday
    • Lecture 2 - Text-Based Graphics Language
  • Wednesday
    • Pre-Recorded Lecture 3 - Compound Procedures (MQ 2)
    • Tutorial 0 (in-class) - Graphics!
    • You'll get a Canvas message notifying you what Tutorial Group you were assigned on Tuesday
  • Friday
    • Lecture 4 - Aligning Language + (in-class) MQ 3