What is a Program?

Programming with Racket

Before you start:

Atoms

Space or line delimited values, or names of something that the computer already knows about:

    123       a numeric value
    1.23      another numeric value
    12 34     two values on one line
    "abc"     a string value
    +         a known function name 
    abc       an unknown name, gives an error

Except the error cases, Racket will answer with the same value as its interpretation of what you have given to it.

Expressions

An expression is an operation, expressed following a certain grammar:

Combined expressions

Once interpreted, an expression evaluates to an tomic value. Thus value of a function call can be used as an input for another function call:

    (* (+ 2 3) (+ 4 5))

Choose 'Advanced Student' as language and try 'Debug' to see step-by-step how the computer interprets the expression above.

Exercise

Express the following as a Racket expression: \(\frac{2^8 + 4}{\frac{1}{32}}\)

More than numbers!

Images

Exercise

PAIR EXERCISE: Draw a house (a triangle above a square) with a door.

Teaching new things to the computer: constants

Teaching new things to the computer: functions

Literate Programs

Programs are not only for computers, but also for humans to read, understand, and improve. So we do the following:

Testing programs

Like every engineering product, programs must be tsted for correctness:

    (check-expect (areaOfCircle 10) (* Pi 100))

Exercise

Pair exercise: Write a function to compute amount of debt, D, after a month when one gets A liras mortgage credit with monthly interest rate F. Make sure it is properly commented (i.e. have a contract) and tested.

Function re-use and modularity of program design

    ;Function: areaOfRing: outerRadius:Number innerRadius:Number -> area:Number
    ;takes outer and inner radius of a ring and returns its area
    (define (areaOfRing outerRadius innerRadius)
        (- (areaOfCircle outerRadius) (areaOfCircle innerRadius)))
    (check-expect (areaOfRing 10 5) (* Pi 75))
    (areaOfRing 10 5)

Exercise

Pair exercise: Write a function using the previous function so that it will also take a third argument, P, for intended monthly payment, and will return whether this debt will ever be paid (i.e. returns a boolean value by checking if monthly payment is strictly greater than monthly interest). Add contract and test.

Exercise

Pair exercise: Hint:Scale function is used to scale images as (scale factor image). Write (1)a function to place half-sized version of a given shape on itself, (2)a function, using the previous one, to place three copies (full, 1/2 and 1/4 sizes) on itself. Add contract and test.

Home assignment