Professional Documents
Culture Documents
IT Department 2020-2021
Term - 1
Course
Programming Languages
Lecture -1
Introduction
2
2. Systematic program design
3
3. Racket
4
3.1. DrRacket Getting started
5
3.2. Simple calculations
1 + 1 is 2
1 + 2 is 3
3 + 2? ….. 5
>(+ 2 2) > (sqr 3)
(+ 2 2) 4 9
>(* 3 3) > (expt 2 3)
(* 3 3) 9 8
>(- 4 2) > (sin 0)
(- 4 2) 2 0
>(/ 6 2) > (cos pi)
(/ 6 2) 3 #i-1.0
6
7
3.3. Nested arithmetic
> (+ 2 (+ 3 4))
9
> (+ 2 3 4)
9
> (+ 2 (+ (* 3 3) 4))
15 (+ (1) (2))
> (+ 2 (+ (* 3 (/ 12 4)) 4))
15
> (+ 1 2 3 4 5 6 7 8 9 0)
45
8
3.4. Text
9
3.5. Truths
10
(and (or (= (string-length "hello world")
(string->number "11"))
(string=? "hello world" "good morning"))
(>= (+ (string-length "hello world") 60) 80))
#false
11
3.6. Images
To insert images such as ( bio , rocket)
Require 2htdp/image
into DrRacket, use the Insert menu.
>
(128,128)
16384
12
3.7. Functions
x = 1 2 3 4 5 6 7 8 9 10
y = 1 4 9 16 25 36 49 64 81 ?
(define (y x) (* x x))
(y 1)
(y 2)
(y 3)
(y 4)
(y 5)
(FunctionName ArgumentExpression)
> (circle 10 "solid" "red")
> (rectangle 30 20 "outline" "blue")
> (overlay (circle 5 "solid" "red")
(rectangle 30 20 "outline" "blue"))
> (overlay (rectangle 30 20 " solid" "blue")
(circle 5 "solid" "red"))
14
(empty-scene 100 60) (28,28)
(place-image 50 10 (empty-scene 100 60))
(place-image 50 20 (empty-scene 100 60))
(place-image 50 30 (empty-scene 100 60))
(place-image 50 40 (empty-scene 100 60))
15
3.8. Animation
require 2htdp/universe
;(picture-of-rocket.v1 40)
(animate picture-of-rocket.v1)
rv1.rkt
(picture-of-rocket.v1 5555)
16
3.9. Many ways to compute
(define (sign x)
(cond
[(> x 0) 1]
+1 if x > 0 [(= x 0) 0]
Sign(x) 0 if x = 0 [(< x 0) -1]))
-1 if x < 0
> (sign 10)
1
conditional > (sign -5)
-1
expression > (sign 0)
0
17
3.10. Animation again
;(picture-of-rocket.v2 70)
(animate picture-of-rocket.v2)
rv2.rkt
18
(define (picture-of-rocket.v3 height)
(cond
[(<= height (- 60 (/ (image-height ) 2)))
(place-image 50 height (empty-scene 100 60))]
[(> height (- 60 (/ (image-height ) 2)))
(place-image 50 (- 60 (/ (image-height ) 2))
(empty-scene 100 60))]))
;(picture-of-rocket.v3 80)
(animate picture-of-rocket.v3)
rv3.rkt
19
(define (picture-of-rocket.v4 h)
(cond
[(<= h (- HEIGHT (/ (image-height R) 2)))
(place-image R 50 h (empty-scene WIDTH HEIGHT))]
[(> h (- HEIGHT (/ (image-height R) 2)))
(place-image R 50 (- HEIGHT (/ (image-height R) 2))
(empty-scene WIDTH HEIGHT))]))
(define R )
(animate picture-of-rocket.v4)
rv4.rkt
20
; constants
(define WIDTH 400)
(define HEIGHT 200)
(define M (empty-scene WIDTH HEIGHT))
(define R )
(define ROCKET-CENTER-TO-TOP(- HEIGHT (/ (image-height R) 2)))
; functions
(define (picture-of-rocket.v5 h)
(cond
[(<= h ROCKET-CENTER-TO-TOP) (place-image R (/ WIDTH 2) h M)]
[(> h ROCKET-CENTER-TO-TOP)(place-image R (/ WIDTH 2)
ROCKET-CENTER-TO-TOP M)]))
(animate picture-of-rocket.v5)
rv5.rkt
21
; properties of the "world" and the descending rocket
(define WIDTH 400)
(define HEIGHT 300)
(define V 5)
(define X 200)
; graphical constants
(define M (empty-scene WIDTH HEIGHT))
(define UFO )
(define UFO-CENTER-TO-TOP
(- HEIGHT (/ (image-height UFO) 2)))
; functions
(define (picture-of-rocket.v6 t)
(cond
[(<= (distance t) UFO-CENTER-TO-TOP)
(place-image UFO X (distance t) M)]
[(> (distance t) UFO-CENTER-TO-TOP)
(place-image UFO X UFO-CENTER-TO-TOP M)]))
(define (distance t)
(* V t))
(animate picture-of-rocket.v6) rv6.rkt
22
(define UFO
(overlay (circle 10 "solid" "green")
(rectangle 40 4 "solid" "green")) )
rv7.rkt
23
4. The Arithmetic
(+ 1 (+ 1 (+ 1 1) 2) 3 (+ 2 2) 5)
==
(+ 1 (+ 1 2 2) 3 4 5)
==
(+ 1 5 3 4 5)
==
18
24
5. The Arithmetic of strings
(string-append "a" "b") == "ab"
(string-append "ab" "c") == "abc"
(string-append "a" " ") == "a "
26
> (circle 10 "solid" "red")
27
7. The Arithmetic of booleans
(define x 2)
(if (= x 0) 0 (/ 1 x))
28
> (= 2 2) > (> 3 4)
#t #f
> (= 2 3) > (< 3 4)
#f #t
> (= ( sqrt 4) 2) > (>= 2 2 3)
#t #f
> (= 2 2 2) > (even? 42)
#t #t
> (= 2 2 3) > (not (even? 43))
#f #t
> ( equal? 2 2) > (exact? (sqrt 4))
#t #t
> (equal? "Wallace" "Wallace") > (exact? (sqrt 5))
#t #f
> (equal? 2 "Joyce") > (boolean? (= 3 5))
#f #t
> (= "Pynchon" "DeLillo") > (number? (+ 3 5))
=: contract violation #t
expected: number? > (string? "hawk")
given: "Pynchon" #t
argument position: 1st > (string<=? "hawk" "handsaw")
other arguments...: #f
29
8. Functions again
• function definitions:
(define (FunctionName Variable ... Variable)
Expression)
(define (f x) 1) unary function
(define (g x y) (+ 1 1)) dubbed binary
(define (h x y z) (+ (* 2 2) 3)) ternary function
30
(define (f x) 1)
> (f 1)
1
> (f "hello world")
1
> (f #true)
1
> (f (circle 3 "solid" "red") ??
> (f)
f:expects 1 argument, found none
> (f 1 2 3 4 5)
f:expects only 1 argument, found 5
31
(+ (ff (+ 1 2)) 2)
(+ (ff 3) 2)
(define (ff a) (* 10 a))
(+ (* 10 3) 2)
(+ 30 2)
> (+ (ff 3) 2)
32
32
> (* (ff 4) (+ (ff 3) 2))
(* (ff 4) (+ (ff 3) 2))
1280
(* (* 10 4) (+ (ff 3) 2))
> (ff (ff 1))
(* 40 (+ (ff 3) 2))
100
(* 40 32)
1280
32
9. Composing functions
(define (letter fst lst signature-name)
(string-append (opening fst) "\n\n" (body fst lst)
"\n\n" (closing signature-name)))
33
Sample Problem The owner of a monopolistic movie theater in
a small town has complete freedom in setting ticket prices. The
more he charges, the fewer people can afford tickets. The less
he charges, the more it costs to run a show because attendance
goes up. In a recent experiment the owner determined a
relationship between the price of a ticket and average
attendance.
At a price of $5.00 per ticket, 120 people attend a performance.
For each 10-cent change in the ticket price, the average
attendance changes by 15 people. That is, if the owner charges
$5.10, some 105 people attend on the average; if the price goes
down to $4.90, average attendance increases to 135. Let’s
translate this idea into a mathematical formula:
34
Sample Problem
attendance = 120 people – ((change in price) /0.10 ) * 15 people
35
10. Global Constants
; a blank line:
(define NL "\n")
; an empty scene:
(define MT (empty-scene 100 100))
36
11. Programs
37
11.1. Batch program
38
11.2. interactive program
(define (number->square s)
(square s "solid" "red"))
(define (reset s ke) 100)
intractive1.rkt
39
Another Sample interactive
program
intractive2.rkt
40