Programming Paradigms
• Course overview
•Introduction to programming paradigms
Copyright By PowCoder代写 加微信 powcoder
• Review: The object-oriented
paradigm in Java •Imperative and concurrent
programming paradigm: Go. • Logic paradigm: Prolog.
• Functional paradigm: Scheme.
Announcement
• comprehensive assignment Prolog is due on April 8th. Accepted late with penalty till April 10th. TA: Ahmed
• Prolog assignment is posted. Due on March 30th , Accepted late till April 1st. TA: Alim and Emmanuel
• Scheme assignment is posted, Due on April 9, TA: Manorama
Announcement
•Thursday March 31 (4:00 -5:20 pm) live Tutorial session for
•Comprehensive Prolog •Comprehensive scheme
•Thursday April 1 (4:00 – 5:20 pm) live Tutorial session for Prolog previous exam.
•Quiz: TA Kamrooz
Acknowledgment
The slides posted through the term are based of the slides offered by:
Prof. Jochen Lang
Demo code: https://www.site.uottawa.ca/~jl ang/CSI2120/demoCode.html
Scheme: Functional Programming
• LocalBinding,let-boundVariables • Namedlet-bounds
• Characters
Local Binding, let-bound Variables:
– to define a list of local variables for a list of expressions – each variable name is bound with a value
– let returns the result of the last expression
• but evaluates all expressions from left to right
(let ((a 2) (b 3)) ; local variables a and b (+ a b)) ; expression where the
; variables are bound
a=> Unbound variable: a b=> Unbound variable: b
https://docs.racket-lang.org/reference/let.html
Local Function Definitions
• let can be used to define local functions
(let ((a 3)
(square (lambda (x) (* x x)))
(plus +)) ; end of definitions
; applied to
(sqrt (plus (square a) (square b)))) => 5
Sequential Definitions with let*
(let ((x 1) (y (+ x 1)))
(list x y))
=> Error: variable x is not bound.
In order to define y in terms of x – the function let* exists
(let* ((x 1) (y (+ x 1)))
(list x y))
let* is similar to let but allows for sequential definitions.
Sequential Definitions with Let, let*
(let* ((x 1) (y (+ x 1)))
(list x y))
• How can we use let only??
(let ((x 1) )
(let ((y (+ x 1)))
(list x y)))
Example using let vs. let*
(let ((x 2) (y 3))
(let ((x 7) ; x = 7
(z (+ x y))) ; z = 2 + 3 (* z x))) ; 5 * 7
(let ((x 2) (y 3))
(let* ((x 7) ; x = 7
(z (+ x y))) ; z = 7 + 3 (* z x))) ; 10 * 7
Setting let-bound Variables
• Let-boundvariablescanbechangedwithset! (define seconds-set
(lambda (h m s)
(let ((sh 0) (sm 0) (total 0))
(set! sh (* 60 (* 60 h)))
(set! sm (* 60 m))
(set! total (+ s (+ sh sm)))
=> seconds-set
(seconds-set 1 5 3)
Same Example in Functional Style
(define seconds
(lambda (h m s)
(let ((sh (* 60 (* 60 h)))
(sm (* 60 m)))
(+ s (+ sh sm)))))
=> seconds
(seconds 1 5 3)
Recursive Definitions with letrec • letrec
– permits the recursive definitions of functions
– letrec is similar let* but all the bindings are within
thescopeof thecorrespondingvariable • Example:Localdefinitionoffactorial
(letrec ((fact (lambda (n)
(if (= n 1)
1(* n (fact (- n 1))))))) (fact 5))
Recursive Application of a Function to a List
• Example:
– The function fct is applied to all elements in a list
(define (apply-f fct L)
(letrec ((app
(lambda (L)
(if (null? L)
(cons (fct (car L)) (app (cdr L))))))) (app L)))
=> apply-f
(define double-ele (lambda(x) (+ x x))) => double-ele
(apply-f double-ele ‘(1 2 3 4))
=> (2 4 6 8)
Named let-bound Variables
• Use of a name in the let expression
(let name ((var val) …) exp1 exp2 …)
– Factorial example
(let ft ((k 5)) (if (<= k 0)
1(* k (ft (- k 1))))) ; call with k=k-1
• is the same as:
(letrec ((name (lambda (var ...) exp1 exp2 ...)) (name val) ...)
(letrec ((ft (lambda (k) (if1(<= k 0)
(* k (ft (- k 1))))))) (ft 5))
Examples: Named let-Bound
• Usedforrecursionsandloops
(define divisors (lambda (n)
(let f ((i 2))
((>= i n) ‘())
((integer? (/ n i))
(cons i (f (+ i 1)))) ; call body with i=i+1
(else (f (+ i 1))))))) ; call body with i=i+1 => divisors
(divisors 32) => (2 4 8 16)
A Further Example
(let loop ((numbers ‘(3 -2 1 6 -5)) (nonneg ‘())
(neg ‘()))
(cond ((null? numbers) (list nonneg neg))
((>= (car numbers) 0)
(loop (cdr numbers) ; 3 arg. for loop
(cons (car numbers) nonneg)
((< (car numbers) 0) ; 3 other arg. for loop
(loop (cdr numbers)
(cons (car numbers) neg))))) => ((6 1 3) (-5 -2))
Store State in a Global with set!
(define num-calls 0)
=> num-calls
(define kons
(lambda (x y)
(set! num-calls (+ num-calls 1))
(cons x y)))
(kons 3 5)
=> (3 . 5)
(display num-calls)
Types Characters
• Characterconstants:
#\a #\A #\( #\space #\newline
• Predicats:
– Mostly obvious
(char? obj) tests whether obj is a character.
(char-alphabetic? char)
(char-numeric? char)
(char-whitespace? char)
(char-upper-case? char)
(char-lower-case? char)
Character Comparisons
• Booleanfunctionsforcharacters:
(char=? char_1 char_2) (char char_1 char_2) (char>? char_1 char_2) (char<=? char_1 char_2) (char>=? char_1 char_2)
• Correspondingcaseinsensitivefunctionswiththe ending –ci exist.
(char=? #\a #\A)
(char-ci=? #\a #\A)
Character Conversions
• Charactertoascii
(char->integer #\a)
• Charactertoasciiandback
(integer->char (1+ (char->integer #\a))) #\b
• Stringconstantsarewrittenindoublequotationmarks
• Booleancomparisonfunctionsforstrings
(string=? string_1 string_2) (string string_1 string_2) (string>? string_1 string_2) (string<=? string_1 string_2) (string>=? string_1 string_2)
• Examples
(string=? “Foo” “foo”)
(string-ci=? “Foo” “foo”)
More String Functions
(string-length “Hello”) => 5
(string->list “Hello”)
=> (#\H #\e #\l #\l #\o) (substring “computer” 3 6) => “put”
(define (abc-count char k) (if (char-alphabetic? char)
(let ((base (if (char-upper-case? char) (char->integer #\A)
(char->integer #\a)))) (integer->char
(- (char->integer char) base))
char)) ; apply let to char
=> abc-count (abc-count #\b 5) #\g
• LocalBinding,let-boundVariables
– let for local variable binding
– let* for sequential local varible binding
– letrec for local variable binding allowing recursions
• Namedlet-bounds • Characters
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com