Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

Kalamoon University

IT Department 2020-2021
Term - 2

Course
Programming Languages

Lecture -3
Intervals, Enumerations,
and Itemizations
Dr. Mohanad Rajab
1. Enumerations
it defines a collection of data as a finite number of pieces of data.

; A TrafficLight is one of the following ; A MouseEvt is one of


Strings: these Strings:
; -- "red" ; -- "button-down"
; -- "green" ; -- "button-up"
; -- "yellow" ; -- "drag"
; interpretation the three strings represent the ; -- "move"
three ; possible states that a traffic light ; -- "enter"
; -- "leave"
; TrafficLight -> TrafficLight
; the next state from current state s
(check-expect (traffic-light-next "red") "green")
(define (traffic-light-next s)
(cond
[(string=? "red" s) "green"]
[(string=? "green" s) "yellow"] (traffic-light-next "red" )
[(string=? "yellow" s) "red"]))

2
2. intervals
is a description of a class of numbers via boundaries.

(define WIDTH 300)


(define HEIGHT 200)
(define MTSCN (empty-scene WIDTH HEIGHT))
(define UFO
(overlay (circle 10 "solid" "green")
(rectangle 40 4 "solid" "green")) )
(define (main y0 )
(big-bang y0
[on-tick nxt]
[to-draw render]))
;(check-expect (nxt 11) 14) Interactive5.rkt
(define (nxt y) (+ y 1))
;(check-expect (render 11) (place-image UFO 500 11 MTSCN))
(define (render y) (place-image UFO 150 y MTSCN))
(main 0)

3
(define W 300)
(define H 210)
(define CL (/ H 3))
(define SC (empty-scene W H))
(define UFO
(overlay (circle 10 "solid" "green")
(rectangle 40 4 "solid" "green")) )
(define (main y0) Interactive6.rkt
(big-bang y0
[on-tick nxt]
[to-draw render/status]))
(define (nxt y)(+ y 1))
(define (put y)(place-image UFO 150 y SC))
(define (render/status y)
(cond
[(<= 0 y CL) (place-image (text "DDD" 11 "green") 40 40(put y))]
[(and (< CL y) (<= y H)) (place-image (text "CCC" 11 "orange") 40 40 (put y))]
[(> y H) (place-image (text "LLL" 11 "red") 40 40 (put y))]))
(main 0)

4
(define W 300)
(define H 210)
(define CL (/ H 3))
(define SC (empty-scene W H))
(define UFO
(overlay (circle 10 "solid" "green")
(rectangle 40 4 "solid" "green")) )
(define (main y0)
(big-bang y0
[on-tick nxt]
[to-draw render/status]))
(define (nxt y)(+ y 1)) Interactive7.rkt
(define (put y)(place-image UFO 150 y SC))
(define (render/status y)
(place-image (cond
[(<= 0 y CL) (place-image (text "DDD" 11 "green"))]
[(and (< CL y) (<= y H)) (place-image (text "CCC" 11 "orange")) ]
[(> y H) (place-image (text "LLL" 11 "red")) ])
40 40 (put y)))
(main 0)

5
(define (show rr)
(cond 3. itemization
[(string? rr) (draw-rocket R-GROUNDED)]
[(<= -10 rr -1) (place-image (text (number->string rr) 20 "red")10 (* 3/4 W)
(draw-rocket R-GROUNDED))]
[(>= rr 0) (draw-rocket (+ rr R-CENTER))]))
(define (draw-rocket rr) (place-image R R-X (- H rr) SC))
(define (launch rr key-event)
(cond
[(string? rr) (if (string=? " " key-event) -10 rr)]
[(<= -10 rr -1) rr]
[(>= rr 0) (- H rr)]))
Generalize intervals and
(define (fly rr) enumerations
(cond
[(string? rr) rr]
(define W 100) ; 28x28
[(<= -10 rr -1) (if (= rr -1) 0 (+ rr 1))]
(define H 200) ;
[(>= rr 0) (+ rr R-VELOCITY)]))
(define SC (empty-scene W H))
(define (main rr)
(big-bang rr
(define R )
[to-draw show]
(define R-CENTER (/ (image-height R) 2))
[on-key launch]
(define R-X (/ W 2))
[on-tick fly 0.5]
(define R-GROUNDED R-CENTER)
))
Interactive8.rkt (define R-VELOCITY 5)
(main "hello")
6
; A Price falls into one of three intervals:
; --- 0 through 1000
; --- 1000 through 10000 Sample Problem : tax
; --- 10000 and above.
; interpretation the price of an item

; Price -> Number


; computes the amount of tax charged for p
(define (sales-tax p) 0)

(check-expect (sales-tax 537) 0)


(check-expect (sales-tax 1000) (* 0.05 1000))
(check-expect (sales-tax 12017) (* 0.08 12017))

(define (sales-tax p)
(cond (sales-tax 1400)
[(and (<= 0 p) (< p 1000)) 0]
[(and (<= 1000 p) (< p 10000)) (* 0.05 p)]
[(>= p 10000) (* 0.08 p)]))

7
Sample Problem How a traffic light functions

(define RAD 10) (define (tl-render cs)


(define SPACE (/ RAD 2) ) (overlay
(define MT (empty-scene (* RAD 10) (* RAD 3))) (beside
(bulb cs 0)
(define RED 0) (square SPACE "outline" "white")
(define GREEN 1) (bulb cs 2)
(define YELLOW 2) (square SPACE "outline" "white")
(bulb cs 1))
(define (tl-next cs) MT))
(cond
[(equal? cs RED) GREEN] (define (conv tl)
[(equal? cs GREEN) YELLOW] (cond
[(equal? cs YELLOW) RED])) [(equal? tl RED) "red"]
[(equal? tl GREEN) "green"]
[(equal? tl YELLOW) "yellow"]))

(define (bulb on c)
(if (equal? on c)
(circle RAD "solid" (conv c))
(circle RAD "outline" (conv c))))

(define (traffic-light-simulation initial-state)


(big-bang initial-state
[to-draw tl-render]
[on-tick tl-next 1]))
(traffic-light-simulation RED)
8
4. Defining Structure Types
expression that creates a posn structure
(make-posn 3 4) whose x-coordinate is 3
and whose y-coordinate is 4.

(define p (make-posn 31 26))


(posn-x p) ; 31
(posn-y p) ; 26

; computes the distance of ap to the origin


(define (distance-to-0 ap) (sqrt (+ (sqr (posn-x ap)) (sqr (posn-y ap)))))
(check-expect (distance-to-0 (make-posn 0 5)) 5)
Ran 3 tests.
(check-expect (distance-to-0 (make-posn 7 0)) 7)
1 of the 3 tests failed.
(check-expect (distance-to-0 (make-posn 3 4)) 7)
(distance-to-0 (make-posn 3 4)) ;5

9
(define-struct StructureName [FieldName ...])

1. one constructor creates structure instances. (make-posn )


2. one selector per field, which extracts the value of the field
from a structure instance; ( posn-x , posn-y )
3. one structure predicate.

• (define-struct movie [title producer year])


• (define-struct person [name hair eyes phone])
• (define-struct CD [artist title price])

(define-struct entry [name phone email])

(make-entry " Lee Hong" "666-7771" "lee@x.me") constructor


entry-name, entry-phone, and entry-email selector
entry? predicate.

10
(define-struct entry [ name phone email ] )
(define p1 (make-entry "Tara Harp" "666-7770" "th@smlu.edu")
(entry-name pl) ; "Tara Harp"

(define-struct ball [location velocity])


(define-struct vel [deltax deltay])
(define ball1 (make-ball (make-posn 30 40) (make-vel -10 5)))
(ball-velocity ball1) ; make-vel -10 5
(vel-deltax (ball-velocity ball1)) ; -10

If posn? is a predicate that decides posns from all other values


We should expect that it produces #false for numbers and #true for ap:

> (posn? ap)


#true > (entry? pl)
> (posn? 42) #true
#false > (entry? 42)
> (posn? #true) #false
#false > (entry? #true)
> (posn? (make-posn 3 4)) #false
#true
11
(define-struct posn1 [x y])
(define MTS (empty-scene 100 100))
(define DOT (circle 3 "solid" "red"))

(define (main p0)


(big-bang p0
[on-tick x+] structure1.rkt
[on-mouse reset-dot]
[to-draw scene+dot]))

(define (x+ p)
(make-posn (+ (posn-x p) 3) (posn-y p)))
(define (scene+dot p)
(place-image DOT (posn-x p) (posn-y p) MTS))
(define (reset-dot p x y me)
(cond
[(mouse=? "button-down" me) (make-posn x y)]
[else p]))

(main (make-posn 12 20))

12
Designing with Itemizations

(define WIDTH 200)


(define HEIGHT 200)
(define SCENE (empty-scene WIDTH HEIGHT "skyblue"))

(define UFO-HEIGHT 20)


(define UFO-WIDTH (* 2 UFO-HEIGHT))
(define UFO (overlay (circle (/ UFO-HEIGHT 2) "solid" "purple")
(ellipse UFO-WIDTH (/ UFO-HEIGHT 2) "solid" "blue")))

(define TANK-HEIGHT 10)


(define TANK-WIDTH (* 2 TANK-HEIGHT))
(define TANK (rectangle TANK-WIDTH TANK-HEIGHT "solid" "olive"))

(define MISSILE-WIDTH 1)
(define MISSILE-HEIGHT 15)
(define MISSILE
(rectangle MISSILE-WIDTH MISSILE-HEIGHT "solid" "red"))

13
;; starting positions
(define UFO-X 100)
(define UFO-Y 50)
(define TANK-X 150)
(define TANK-Y (- HEIGHT (/ TANK-HEIGHT 2)))

(define TANK-SPEED 3)
(define UFO-SPEED 3)
(define MISSILE-SPEED 3)
(define JUMP 3) ; UFO randomly jumps by this amount

;; a mock-up
(define MOCK-UP (place-image UFO UFO-X UFO-Y
(place-image TANK TANK-X TANK-Y SCENE)))

(define-struct tank [loc vel])


(define-struct aim [ufo tank])
(define-struct fired [ufo tank missile])

14
(define (si-render s)
(cond
[(aim? s) (tank-render (aim-tank s) (ufo-render (aim-ufo s) SCENE))]
[(fired? s) (tank-render (fired-tank s) (ufo-render (fired-ufo s) (missile-render (fired-missile s) SCENE)))]))

(define (tank-render t im) (place-image TANK (tank-loc t) TANK-Y im))


(define (ufo-render u im) (place-image UFO (posn-x u) (posn-y u) im))
(define (missile-render m im) (place-image MISSILE (posn-x m) (posn-y m) im))

(define (si-game-over? s)
(cond
[(aim? s) (ufo-landed? (aim-ufo s))]
[(fired? s) (or (ufo-landed? (fired-ufo s)) (ufo-hit? (fired-ufo s) (fired-missile s)))]))

(define (ufo-landed? u) (>= (posn-y u) (- HEIGHT (/ UFO-HEIGHT 2))))

(define (ufo-hit? u m) (and (<= (abs (- (posn-x u) (posn-x m)))


(+ (/ UFO-WIDTH 2) (/ MISSILE-WIDTH 2))) (<= (abs (- (posn-y u) (posn-y m)))
(+ (/ UFO-HEIGHT 2) (/ MISSILE-HEIGHT 2)))))

(define (si-move w) (si-move-proper w (random 3)))

(define (si-move-proper s r)
(cond
[(aim? s) (make-aim (update-ufo (aim-ufo s) r) (update-tank (aim-tank s)))]
[(fired? s) (make-fired (update-ufo (fired-ufo s) r) (update-tank (fired-tank s))
(update-missile (fired-missile s)))]))

15
(define (update-ufo u r)
(cond
[(= r 0) (make-posn (posn-x u) (+ (posn-y u) (- UFO-SPEED JUMP)))]
[(= r 1) (make-posn (- (posn-x u) JUMP) (+ (posn-y u) UFO-SPEED))]
[(= r 2) (make-posn (+ (posn-x u) JUMP) (+ (posn-y u) UFO-SPEED))]))

(define (update-missile m) (make-posn (posn-x m) (- (posn-y m) MISSILE-SPEED)))


(define (update-tank t) (make-tank (+ (tank-loc t) (tank-vel t)) (tank-vel t)))
(define (si-control sg ke)
(cond
[(key=? ke "left") (tank-left sg)]
[(key=? ke "right") (tank-right sg)]
[(key=? ke " ") (fire-missile sg)]
[else sg])) ; ignore rest
(define (tank-left s)
(cond
[(aim? s) (make-aim (aim-ufo s) (tank-turn-left (aim-tank s)))]
[(fired? s) (make-fired (fired-ufo s) (tank-turn-left (fired-tank s)) (fired-missile s))]))

(define (tank-turn-left t)
(cond
[(<= 0 (tank-vel t)) (make-tank (tank-loc t) (- 0 (tank-vel t)))]
[else t]))

16
(define (tank-right s)
(cond
[(aim? s) (make-aim (aim-ufo s) (tank-turn-right (aim-tank s)))]
[(fired? s) (make-fired (fired-ufo s) (tank-turn-right (fired-tank s)) (fired-missile s))]))
(define (tank-turn-right t)
(cond
[(< (tank-vel t) 0) (make-tank (tank-loc t) (- 0 (tank-vel t)))]
[else t]))

(define (fire-missile s)
(cond
[(aim? s) (make-fired (aim-ufo s) (aim-tank s) (make-posn (+ (tank-loc (aim-tank s))
(tank-vel (aim-tank s))) (- HEIGHT (/ TANK-HEIGHT 2))))]
[(fired? s) (make-fired (fired-ufo s) (fired-tank s) (make-posn (+ (tank-loc (fired-tank s))
(tank-vel (fired-tank s))) (- HEIGHT (/ TANK-HEIGHT 2))))]))

(define (main ws)


(big-bang ws
(on-tick si-move 0.2)
(to-draw si-render)
(stop-when si-game-over?)
;(on-mouse ...)
(on-key si-control)))

(main (make-aim (make-posn 50 00) (make-tank 20 3)))

17
Vocabulary
Beginning student language
(Implements by racket)

 vocabulary and a grammar


 syntax - semantics
 A sentence in BSL is an expression or a definition.

 Name or variable is a sequence of characters, not including a space


or one of the following: " , ' ` ( ) [ ] { } | ; #:
 A primitive is a name : + or sqrt.
 A variable is a name without preassigned meaning: 1, -1, 3/5, 1.22,
 A Boolean is one of: #true or #false.
 A string is one of: "", "he says \"hello world\" to you",…..
 An image is a png, jpg, tiff, …..

18
grammar

program = def-expr …
def-expr = def
| expr

def = (define (variable variable variable ...) expr)

expr = variable
| value
| (primitive expr expr ...)
| (variable expr expr ...)
| (cond [expr expr] ... [expr expr])
| (cond [expr expr] ... [else expr])

19
Syntactic naming conventions

; function application:
(function argument ... argument)

; function definition:
(define (function-name parameter ... parameter)
function-body)

; conditional expression:
(cond
cond-clause
...
cond-clause)

; cond clause
[condition answer]

20

You might also like