CS代考 CSI2120/demoCode.html

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
• Quiz 3 is posted due Thursday Feb 14th
• Extra quiz in Go is posted (Best 9 will be sued toward your mark).
Labs will be offered in-person and remote starting Feb 14th

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

Announcement
• Recorded lecture?
• Go concurrent visualize execution

Arithmetic Expressions and I/O
• ArithmeticExpressions
– Built-in operators
– Unification with numbers – Recursive calculations
– Generator

Numbers in Prolog
• Prolog recognizes numbers – integers and floating point
• Number constants
5 1.75 0 1.345e-10 -27 -3.4 42
• Rules about arithmetic expressions use – number constants
– arithmetic operators
– arithmetic variables

Arithmetic Expressions
• Prolog supports common operators as built-ins including
X // Y %integer division X mod Y
• Mathematical functions, e.g.,

Evaluating Arithmetic Expressions
• Specialpredicate“is”inordertotreatvariablesand operators as relating to mathematical operations
?- 1+2 = 1+2.
?- 3 = 1+2.
?- 1+2 = 2+1.
?- 3 is 1+2.
?- X is 1+2, X is 2+1.

Unification with Arithmetic Expressions
• Careful with expressions and unification – Unification of 1+2 and 3 fails.
• 3 is a number, while 1+2 is a term.
– Evaluation of arithmetic expression is not part of the regular unification algorithm and does not happen automatically

Infix Comparison Operators
• Comparisons
X=:=Y %XequalsY X=\=Y %XnotequalsY X=Y.
• Whatqueriescanweask?
?- min(5,7,X).
?- min(5,X,7). % false
?- min(X,5,7). % false
?- min(X,Y,7). % error – why?

Predicates using Recursion: power
• PositivePowers
– boundary case for power to 1
pow( X, 1, X ).
– recursion to calculate the product
pow( X, Y, Z ) :- Y > 1,
Y1 is Y – 1,
pow( X, Y1, Z1 ), Z is X * Z1.

Predicates using Recursion: gcd
• Greatestcommondivisor – Boundary condition
• gcd of 0 and any number is the number itself
gcd(U,0,U).
– Recursive clause based on Euclid’s algorithm
• modulodivisionsuntilremainderis0atwhichpoint we found a divisor for all intermediate divisors and the original number
gcd(U,V,W) :- V>0, R is U mod V, gcd(V,R,W).
• AlternativeimplementationofEuclid’salgorithm
gcd(A,A,A).
gcd(A,B,GCD):- AB, NA is A-B, gcd(NA,B,GCD).

Animation of Euclid’s Algorithm
gcd(1071,462,W) :-
462>0, 147 is 1071 mod 462,
gcd(462,147,W) :-
147>0, 21 is 462 mod 147,
gcd(147,21,W) :-
21>0, 0 is 147 mod 21,
gcd(21,0,W). W = 21.
Image source: Wikimedia Commons, CC 3.0, Author: Proteins

Predicates using Recursion: fibonacci
• Fibonaccinumbers
– aseriesofnumbers1123581321…
– RecursiveclausebasedonFibonacci’salgorithm • fib(N)=fib(N-1)+fib(N-2)
fib(N,F):- N>1,
N1 is N-1,
N2 is N-2, fib(N1,F1), fib(N2,F2), F is F1+F2.
– Two boundary conditions are needed.

Example with Crossed Recursions
Predicate to test if a positive number is even
even(0). odd(N) :- N>0,
even(M). even(N):- N>0,
M is N-1, odd(M).

A Last Example
• IntervaltesttoseeifXisintheintervalbetweenLandH
intervalTest(X,L,H):- X>=L, X=CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com