PA2+-+Pascal+2

Doc. RNDr. Pavel Satrapa, Ph. D. Email: pavel.satrapa (at) tul.cz Tel: +420 48 535 3685 Office: A9
 * Programming paradigms 2**


 * Some useful links for our PAP course:** (Satrapa's presentations)

[|Programming paradigms 1] [|http://www.kai.vslib.cz/~satrapa/en/programming/]

[|Programming paradigms 2] [|http://www.kai.vslib.cz/~satrapa/en/paradigms/]

http://scheme.com/resources.html** http://schemers.org/
 * Links to Scheme Resources

R. Kent Dybvig ** http://scheme.com/tspl3/
 * The Scheme-Programming Language

Modules (units)
 * Lesson #1:**

Objects (OOP)
 * Lesson #2:**

(SWI-Prolog)
 * Lesson #3:**

parent(alfons,frank). parent(alfons,vera). parent(emily,frank). parent(emily,vera).

parent(frank,bob). grandparent(alfons,bob).

parent(ann,nicol).

parent(ann,mark). parent(mark,jane). kokot(mark,margoska).

grandparent(X,Y) :- parent(X,Z), parent(Z,Y).

ancestor(X,Y) :- parent(X,Y). ancestor(X,Y) :- parent(X,Z), ancestor(Z,Y).

Download a new program [|DrScheme] and have fun. Choose the program language - Teaching language - Intermediate Student. Play a little bit with the basic function in the down part. [|DrSchem the begining]. [|How to designe the program].
 * Lesson #4:**

Next step is to try out the uper part. Write: //(define(twice x) (* 2 x))

(twice 5) (* 2 (+ 4 6)) (+ 1 2 3)// and check out function like Step, Check Syntax, Run, Stop. If you need some help click on help. Other examples: //(sqrt (+ (expt 10 2) (expt 15 2))) or (< (expt 2 10) (expt 10 2)) or (max (expt 2 10) (expt 10 2)) or (list (expt 2 10) (expt 10 2))//


 * Lesson #5:**

Examples:

code (define (circlearea r) (* 3.14 (* r r))) (define (ringarea outer inner) (- (circlearea outer) (circlearea inner))) (ringarea 8 2) code

 ... result=188.4 

code (define (quadeq a b c) (list (root1 a b c) (root2 a b c))) (define (root1 a b c) (/ (+ (- b) (sqrt (discrim a b c))) (* 2 a))) (define (root2 a b c) (/ (- (- b) (sqrt (discrim a b c))) (* 2 a))) (define (discrim a b c) (- (* b b) (* 4 a c))) (quadeq 1 1 -12) code

... result= (list 3 -4)

code (define (hypothenuse a b) (sqrt (+ (* a a) (* b b)))) (hypothenuse 3 4) code

... result= 5 

code (define (lsecond L) (car (cdr L))) (define (last L) (car (reverse L))) (define (lastcdr L) (reverse (cdr (reverse L)))) (define (skipsecond L) (cons (car L) (cdr(cdr L)))) (define (skipsecond2 L) (append (list (car L)) (cdr(cdr L))))
 * second option for skipsecond

(define (swap L) (append (cdr L) (list(car L)))) (define (swap2 L) (cons (car(cdr L)) (list (car L))))
 * second option for swap

(lsecond '(1 2 3 4 5)) (last '(1 2 3 4 5)) (lastcdr '(1 2 3 4 5)) (skipsecond '(1 2 3 4 5)) (skipsecond2 '(1 2 3 4 5)) (swap '(1 2)) (swap2 '(1 2)) code

Results: 2 5 (list 1 2 3 4) (list 1 3 4 5) (list 1 3 4 5) (list 2 1) (list 2 1)


 * Lesson #5:**

Examples:

//Create boolean function (inbetween a b c) a<=b<=c//

code (define (inbetween a b c) (cond [(and (<= a b) (<= b c)) #t] [else #f] )) (inbetween 1 2 3) code

or

code (define (inbetween a b c) (and (<= a b) (<= b c) #t)) (inbetween 1 2 3) code

 ... result= true 

//Create numeric function which limits x to the interval from min to max//

code (define (limiter min x max) (cond [(< x min) min] [(> x max) max] [else x])) (limiter 0 19 100) (limiter 0 -5 100) (limiter 0 250 100) code

Results 19 0 100

code (define (sumlist L) (cond [(empty? L) 0] [else (+ (car L) (sumlist(cdr L)))] )) (define (poslist L) (cond [(empty? L) 0] [(< (car L) 0) (poslist(cdr L))] [else (+ (car L) (poslist(cdr L)))] )) (sumlist '(-1 -1)) (poslist '(-1 -1 5)) code
 * sumarize the positive list

 Results -2 5

HW task! -create a function (last L) returning the last element of the list L.. DONT USE THE REVERSE FUNCTION


 * Lesson #6:**

Examples:

code (define-struct ware (name price amount)) (define depot (list (make-ware "Coke" 12 100) (make-ware "Pepsi" 11 200) (make-ware "Sprite" 10 300))) (define (find-price name L) (cond [(empty? L) 0] [(string=? name (ware-name (car L))) (ware-price (car L))] [else (find-price name (cdr L))] ))
 * Create a function named depot-value which calculates the global value of all wares
 * in the depot

(define (depot-value L) (cond [(empty? L) 0] [else (+ (global-price (car L))            (depot-value (cdr L)))] ))

(define (global-price W) (* (ware-price W) (ware-amount W)))

(find-price "Coke" depot) (depot-value depot) code

 Results 12 6400

HW task! -create a function code code
 * create function (bill-price order), the order is a list of lists
 * (("apple" 2) ("tomato" 1)) - single sublist for every ware, contains the name ans the
 * number of pieces purchased


 * Lesson #7:**

Examples:

code (define (findsecond Max SecMax L) (cond [(empty? L) SecMax] [(<= Max (car L)) (findsecond (car L) Max (cdr L))] [(<= (car L) SecMax) (findsecond Max SecMax (cdr L))] [else (findsecond Max (car L) (cdr L))] ))

(define (max-second L) (findsecond (max (car L) (second L)) (min (car L) (second L)) (cdr (cdr L)))) (max-second '(15 10 20 17 8)) (second (quicksort '(15 10 20 17 8) >)) code
 * quickest solution for lazy students

 Results <span style="color: rgb(83, 196, 28)">17 17

code (define (nearest_to_avg L) (cond [(empty? L) #f] [else (nearest (car L)                  (avg L)                   (cdr L))] ))

(define (avg L) (/(sum L) (length L))) (define (sum L) (cond [(empty? L) 0] [else (+ (car L) (sum(cdr L)))] ))

(define (nearest Candidate Target L) (cond [(empty? L) Candidate] [(< (abs (- Candidate Target))       (abs (- (car L) Target))) (nearest Candidate Target (cdr L))] [else (nearest (car L) Target (cdr L))] ))

(nearest_to_avg '(6 10 15 5)) code

<span style="color: rgb(83, 196, 28)"> Results <span style="color: rgb(83, 196, 28)">10

HW task!

quick sort for the last task...

1. Create function (f a b) evaluateing ((a/b) + (b/a)) /(a*b)
 * Demo exam:**

2. Create function (select n L) selecting n/th element from list L (if n> length (L) return #f)

3. We have following definition of department: (define-stuct dept (name employees)) where department name is a string and employees is a list of strings containing names of employees working in the departmet. Create function (isemployee name department) which returns #t if department contains employee with the given name.

Results:

(define (f a b) (/ (+ (/ a b) (/ a b)) (* a b))) (f 4 2)
 * 1 Task
 * should be 0.5


 * 2 Task

(define (select n L) (cond [(empty? L) "the number is not in the list"] [(> n (length L)) ((length L))] [(<= n 1) n) (car L)] [else (select (- n 1) (cdr L))]

))

(select 7 '( 2 3 4 5 6 7))

(define-struct dept (name employees)) (define (isempoyee name department) (searchstring name ( dept-employees department))) (define (searchstring s L) (cond [(emty? L)#f] [(string=? s (car L)) #t] [else (searchstring s (cdr))] ))
 * 3 Task