Lecture 1 Slides - Lecture Slides

1 of 62

Lecture 1

Programming in Text

2 of 62

Big ideas from Data Flow

  • 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

3 of 62

Review 0. (not a question)

4 of 62

Review 1.

5 of 62

exceptions

6 of 62

What’s the output of this diagram?

7 of 62

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
  • 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

8 of 62

What about this one?

9 of 62

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

10 of 62

What about this one?

11 of 62

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

12 of 62

Type signatures

13 of 62

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

14 of 62

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

15 of 62

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

16 of 62

Functions / Programs as data

  • Remember that we said functions are really just data, and so can be inputs of other functions
  • Static checkers are an example of this
    • Our broken program (as a whole) was input to the static checker
    • The output was a diagnosis of the problem

“type error in

call to prefix”

check

17 of 62

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

18 of 62

The�language question

19 of 62

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

20 of 62

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

21 of 62

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”

22 of 62

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

23 of 62

24 of 62

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

25 of 62

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

26 of 62

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

27 of 62

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"

28 of 62

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*

29 of 62

So that means…

30 of 62

So that means…

constant (number)

constant (number)

variable

(name: +)

(value: the plus function)

31 of 62

How do we call or execute a function?

constant (number)

constant (number)

variable

(name: +)

(value: the plus function)

32 of 62

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

33 of 62

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)

34 of 62

Equivalent diagrams

(+ 1 2) means:

or really:

35 of 62

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

36 of 62

extra

exercises

37 of 62

Which of these has problems?

38 of 62

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)

39 of 62

Which of these has problems?

40 of 62

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)

41 of 62

Chained (nested) calls

  • The basic call format is:

(function input1 … inputn)

  • But any of these can be calls themselves
  • This means the output of the inner call is used as the input to the enclosing call
  • Calls are chained by nesting (chaining) their expressions

Examples

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

"this "� (string-append "is a "

"test"))

  • (square 10 "solid" "blue")
  • (above (square 10� "solid"

"blue")

(square 10� "solid"

"red"))

42 of 62

There’s always exactly one call for every pair of parens

Parentheses always mean call (for now)

43 of 62

There’s always exactly one call for every pair of parens

Parentheses always mean call (for now)

(+ 1 2)

((+ 1 2))

(+ (1) 2)

44 of 62

There’s always exactly one call for every pair of parens

Parentheses always mean call (for now)

(+ 1 2)

((+ 1 2))

(+ (1) 2)

45 of 62

Function position vs argument position

  • The function to call is always the first item after the parens
  • The rest of the items are always inputs

  • Placing something that’s not a function at the beginning causes a not a function exception
  • (+ 1 2)
    • Call + with 1 and 2 as inputs
  • (1 + 2)
    • Call 1 with + and 2 as inputs
    • Not a function exception
  • (+ (+ 1 2) 3)
    • Call + with 1 and 2 as inputs
    • Call + again with previous result and 3 as inputs
  • ((+ 1 2) 2)
    • Call 3 (the output of(+ 1 2)) with 2 as an input
    • Not a function exception

46 of 62

What are the text versions of these DFDs?

47 of 62

What are the text versions of these DFDs?

(string-append

(string-append "this "

"is a ")

"string")

(+ (+ 1 2) 4)

48 of 62

What’s the text for this DFD?

49 of 62

What’s the text for this DFD?

(string-append (string-append (string-append "this " "is a ") "tedious ") (string-append "example of " "chaining"))

50 of 62

Subexpressions and data flow

51 of 62

Subexpressions and data flow

(string-append (string-append (string-append "this " "is a ") "tedious ") (string-append "example of " "chaining"))

52 of 62

Running code�in DrRacket

53 of 62

Using DrRacket

  • The top pane is the Definitions Window
    • Code here doesn’t run until you choose Run from the Racket menu
  • The bottom pane is the Interactions Window
    • Aka a REPL (Read/Evaluate/Print Loop)
    • You can type expressions here at the “>” prompt
    • Racket will run them when you hit enter and print the result

54 of 62

Our Style Rules for this Class

  • Don’t write you code as one long line
    • Add line breaks between arguments in complex calls
  • Always keep your code indented properly
    • Press tab to ask Racket to reindent a line based on the surrounding parentheses or Ctrl-I / Command-I to reindent the whole file
    • If things that should be inputs to the same function don’t line up, then your parentheses are wrong

55 of 62

What to do when it breaks

  • Don’t stress. Broken code is perfectly normal.
  • Find the code highlighted in pink. This is the specific expression that generated the error
  • Now read the error message carefully; it’s telling you what went wrong when running the pink expression
  • If you see code that's highlighted in black (and orange text), that means that code was not run (we probably won't see that until Wednesday)

56 of 62

Making it legible

(string-append (string-append (string-append "this " "is a " "tedious ") (string-append "example of " "chaining")))

  • This is completely illegible
  • You can’t read, yet alone understand, code that’s written like this
  • Aesthetics have functional consequences!

57 of 62

Making it legible

(string-append (string-append (string-append "this " "is a " "tedious ") (string-append "example of " "chaining")))

  • This is completely illegible
  • You can’t read, yet alone understand, code that’s written like this
  • Aesthetics have functional consequences!

58 of 62

Making it legible

(string-append (string-append (string-append "this "

"is a "

"tedious ")

(string-append "example of "

"chaining")))

  • To make it legible, we:
    • Break it into multiple lines
    • Indent it to align inputs to the same call

  • In other words, we put it in outline form

59 of 62

Making it legible

(string-append (string-append (string-append "this "

"is a ")

"tedious ")

(string-append "example of "

"chaining"))

  • To make it legible, we:
    • Break it into multiple lines
    • Indent it to align inputs to the same call

  • In other words, we put it in outline form

60 of 62

(string-append (string-append (string-append "this "

"is a ")

"tedious ")

(string-append "example of "

"chaining"))

Indented code is a DFD (just written right to left)

61 of 62

Making it mutually intelligible

  • The computer ignores
    • Line breaks
    • Extra whitespace
    • It only looks at the parens
  • Humans ignore
    • The parens (or at least we stink at reading them)
    • We look at the indentation
  • Note: this means it’s critical the indentation match the parentheses!

62 of 62

Keeping your code indented

  • DrRacket will automatically indent lines
    • When you hit return/enter (indents the new line)
    • When you hit tab (reindents current line)
    • When you do Command-I (on a Mac) / Control-I (on Windows)

  • If you run into a problem, one of the first things to do is to have racket reindent your code
    • Make sure that you and Racket have the same idea of what is an input to what
  • Course staff will NOT look at your code if you haven't correctly indented / formatted