# What is a Program?

• is a precise description of an information processing machine

• simulated by the computer

• built with arithmetic and logic parts

• its design follows same principles, but its construction is implemented with a particular programming language

# Programming with Racket

Before you start:

• Download a copy of the book "How to Design Programs": http://htdp.org

• These slides, some relevant lecture notes and link to video lectures (in Turkish) are available at http://e-skolastik.appspot.com/MehmetGencer (find section 'Work in Progress'). These are all rolling content, and may not be complete!

# 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:

• space between atoms, first atom is the function name, rest are called the inputs or parameters:

(+ 2 3)
• delimit commands with paranthesis, preferably in separate lines:

(+ 2 3)
(abs -3)
(expt 2 32)
• when run, a function call is evaluated and returns a single value

• The list of primitive/built-in functions are available at racket-lang.org website, click How To Design Programs Languages ( http://download.racket-lang.org/docs/5.1/html/htdp-langs/beginner-abbr-prim-ops.html )

# 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!

• Functions can consume inputs or produce outputs whose values are something else than numbers:

(> 2 3)
(even? 3)
(and (> 3 2) (< 3 5))
(string-length "abc")

# Images

• Activate "image" teachpack

• Try the following:

(circle 100 "solid" "red")
(overlay
(circle 100 "outline" "red")
(square 250 50 "red"))
(above
(circle 100 "outline" "red")
(triangle 150 "solid" "red"))
(beside
(circle 100 "solid" "red")
(triangle 150 "solid" "black"))
(place-image
(circle 100 "solid" "red")
10 40
(rectangle 200 300 "solid" "green"))
(beside
(above
(circle 100 "solid" "red")
(triangle 150 "solid" "black"))
(rectangle 200 300 "solid" "green"))

# Exercise

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

# Teaching new things to the computer: constants

• define command takes two parameters: first is 'what is being defined' and second is 'its value':

 (define Pi 3.1418)
• Now we can use it to compute area of a circle as $$\pi r^2$$, for a circle of radius 10:

(* Pi 10 10)

# Teaching new things to the computer: functions

• If the first parameter of define is a function call:

(define (functionName parameter1 parameter2 etc)
...
• The second parameter is an expression which uses parameters to produce a value:

(define (areaOfCircle radius)
(* Pi radius radius))
• Now we can use the function:

 (areaOfCircle 10)

# Literate Programs

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

• Choose expressive names for functions and parameters. e.g. radius instead of r

• Indent our expressions. e.g not the following :

    (define (areaOfCircle radius) (* Pi radius radius))

or

        (define
(* Pi
radius radius))

which are all equivalent for the computer!

;Function: areaOfCircle: radius:Number -> area:Number
;takes radius of a circle and returns its area
(* Pi radius radius))

# 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
(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

• Try all the exercises
• Read chapters 1 through 3 from textbook