Lecture 19 Slides - Imperative Programming Pt 2

1 of 12

Lecture 19

Imperative Programming - pt2

2 of 12

#lang htdp/asl

All the code for the rest of the quarter will be in the Advanced Student Language with the exception of Ex.6

3 of 12

Types and Type Signatures in imperative programs

  • It’s now possible to have functions that
    • Take no inputs
    • And/or generate no outputs
  • So we need type signature notations for these

-> number�-> string

  • Means a function that takes no inputs and returns a number, string, etc.

number -> void�string -> void

  • Means a function that takes a number and returns no output

4 of 12

The simplest possible imperative

; void : -> void�

(void)

  • Sometimes, you need a function that literally does nothing
    • No inputs
    • No outputs
    • No effects
  • It’s just a placeholder.
  • Most of the time, we use it to indicate a function returns no value and is instead being run for its side-effects

5 of 12

Assignment statements

(set! name new-value)

The next simplest imperative is assignment (set!) often read "set bang"

  • After execution, the variable name is changed to have the value new-value
  • Variable must have already been “created” using define

6 of 12

Composing imperatives

  • Sequencing
    • begin
  • Conditionals
    • if, when, unless, cond
  • Repetition
    • for-each
    • recursion

7 of 12

Sequencing with begin

(begin expression-1 expression-2)

  • Runs expressions in sequence
  • Returns the value of the last expression automatically
  • Equivalent of { }’s in Java, C++, etc.

8 of 12

Looping as a sequencing primitive

  • Most imperative languages have special iteration constructs
    • while
    • for
    • foreach
  • In ASL, you can just use functions
    • for-each is a useful example

(for-each func a-list)

  • Exactly like map in that it runs func repeatedly on the elements of a-list
  • But returns no value so we use it when you’re calling func as an imperative

9 of 12

Conditionals for Imperative Programming

  • You can use if to choose between two imperatives.
  • You can use cond to choose between many imperatives
  • You can also use when or unless if you only have one imperative and you’re choosing between
    • Running it
    • Not running anything

(if test

imperative

other-imperative)

(when test

imperative)

(unless test

imperative)

10 of 12

Rules of computation in ASL

Look at the expression

  • If it’s a constant (i.e. a number or string), it’s its own value
  • If it’s a variable name (i.e. a word, hyphenated phrase, etc.), look its value up in the dictionary
  • Parentheses? (Check if it’s one of the special cases from the next slide)
  • Otherwise it’s a function call
    • (func-expression arg-expression1arg-expressionn)
    • Execute all the subexpressions�(func-expression and arg-expression1 through arg-expressionn)
    • Call the value of func-expression, passing it the values of arg-expression1 through arg-expressionn as inputs
    • Use its output as the value of the expression

11 of 12

Special forms: New Imperative Additions

If it starts with λ or lambda:

(λ (name1namelast) result-expression)

  • Make a function
    • That names its inputs in the dictionaryname1namelast
    • Returns the value�of result-expression(presumably using those names)
    • Sets the dictionary back the way it was

If it starts with the the word local

(local [(define name1 value1) � …� (define namelast valuelast)]� result-expression)

  • Run the value expressions
  • Assign their values to their respective�names in the dictionary
  • Substitutes their values for their respective�names in result-expression
  • Runs the substituted result-expression
  • Returns its value
  • Set the dictionary back the way it was

12 of 12

Imperative special forms

If it starts with set!

(set! name value-expression)

  • Run value-expression
  • Assign its value to namein the dictionary

If it starts with the the word begin

(begin expressions )

  • Run the expressions
  • Return the value of the last one

If it starts with when or unless:

(when test expression)

  • Run test
  • If its value is true, run expression
  • unless is the same, but runs expression when test is false