Skip to content

Commit f58eec2

Browse files
committed
Initial commit
0 parents  commit f58eec2

File tree

12 files changed

+174
-0
lines changed

12 files changed

+174
-0
lines changed

Diff for: .gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
*~
2+
#*
3+
.idea/
4+
.swap

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
##Paradigms of Computer Programming
2+
------
3+
Quizzes and homeworks for edX course **Paradigms of Computer Programming**
4+

Diff for: invariant_programming_and_lists/append.oz

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare
2+
fun {AppendLists L1 L2}
3+
case L1 of
4+
nil then L2
5+
[] H|T then H|{AppendLists T L2}
6+
end
7+
end
8+
9+
{Browse {AppendLists [4] [1 2 3]}}
10+
{Browse {AppendLists [5 [6]] [1 2 3]}}

Diff for: invariant_programming_and_lists/factorial.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""
2+
The script below shows that Python does not support tail call optimization.
3+
"""
4+
from memory_profiler import profile
5+
6+
7+
def fact(n):
8+
if n == 0:
9+
return 1
10+
else:
11+
return n * fact(n - 1)
12+
13+
14+
def fact_tail(n, a=1):
15+
if n == 0:
16+
return a
17+
else:
18+
return fact_tail(n - 1, n * a)
19+
20+
21+
def fact_loop(n, a=1):
22+
while True:
23+
if n == 0:
24+
return a
25+
n, a = n - 1, n * a
26+
27+
28+
@profile
29+
def main(n):
30+
#a = [fact_tail(i) for i in xrange(n)]
31+
b = [fact(i) for i in xrange(n)]
32+
#c = [fact_loop(i) for i in xrange(n)]
33+
34+
if __name__ == '__main__':
35+
main(500)

Diff for: invariant_programming_and_lists/find_in_list.oz

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
declare
2+
fun {Prefix L1 L2}
3+
case L1 of
4+
nil then true
5+
[] H|T then
6+
if L2 == nil then false
7+
else H==L2.1 andthen {Prefix T L2.2} end
8+
end
9+
end
10+
11+
declare
12+
fun {FindString L1 L2}
13+
if L2 == nil then {Prefix L1 L2}
14+
else
15+
if {Prefix L1 L2} then true
16+
else {FindString L1 L2.2} end
17+
end
18+
end
19+
20+
{Browse {Prefix [1 2] [1 2 3]}}
21+
{Browse {Prefix nil [1 2 3]}}
22+
{Browse {Prefix [1] nil}}
23+
{Browse {FindString [1 3] [1 2 3]}}
24+
{Browse {FindString nil nil}}

Diff for: invariant_programming_and_lists/flatten_list.oz

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
declare
2+
fun {FlattenList L}
3+
case L of
4+
nil then L
5+
[] H|T then
6+
case H of
7+
HH|HT then {Append {FlattenList H} {FlattenList T}}
8+
else H|{FlattenList T}
9+
end
10+
end
11+
end
12+
13+
{Browse {FlattenList [[1 2] [1 [2]]]}}
14+
{Browse {FlattenList [[1 2 [3 [4[5]]]] [1 [2]]]}}

Diff for: invariant_programming_and_lists/list_factorial.oz

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare
2+
fun {ListFact L}
3+
fun {Fact N Acc}
4+
if N<2 then Acc
5+
else {Fact N-1 N*Acc} end
6+
end
7+
in
8+
case L of
9+
nil then nil
10+
[] H|T then {Fact H 1}|{ListFact T}
11+
end
12+
end
13+
14+
{Browse {ListFact [1 2 3]}}
15+
{Browse {ListFact [7 6 15]}}

Diff for: invariant_programming_and_lists/mirror.oz

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare
2+
fun {MainMirror N}
3+
fun {Mirror N Acc}
4+
if N==0 then Acc
5+
else {Mirror (N div 10) Acc*10+(N mod 10)} end
6+
end
7+
in
8+
{Mirror N 0}
9+
end
10+
11+
{Browse {MainMirror 1234}}
12+
{Browse {MainMirror 876465}}

Diff for: invariant_programming_and_lists/naive_fib.oz

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
declare
2+
fun {NaiveFib N}
3+
case N of
4+
0 then 0
5+
[] 1 then 1
6+
else {NaiveFib N-1} + {NaiveFib N-2}
7+
end
8+
end
9+
10+
{Browse {NaiveFib 32}}

Diff for: invariant_programming_and_lists/prime.oz

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
declare
2+
fun {Prime N}
3+
fun {IsPrime N Acc}
4+
if Acc==1 then true
5+
else
6+
if N mod Acc == 0 then false
7+
else {IsPrime N Acc-1} end
8+
end
9+
end
10+
in
11+
if N==1 then false
12+
else {IsPrime N N-1} end
13+
end
14+
15+
16+
{Browse {Prime 1}}
17+
{Browse {Prime 2}}
18+
{Browse {Prime 13}}
19+
{Browse {Prime 50}}

Diff for: invariant_programming_and_lists/sum.oz

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
declare
2+
fun {MainSum N}
3+
fun {Sum N Acc}
4+
if N==0 then Acc
5+
else {Sum N-1 N+Acc} end
6+
end
7+
in
8+
{Sum N 0}
9+
end
10+
11+
{Browse {MainSum 10}}
12+
{Browse {MainSum 5}}

Diff for: invariant_programming_and_lists/tail_fib.oz

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
declare
2+
fun {TailFib N}
3+
fun {Fib N Acc1 Acc2}
4+
case N of
5+
0 then Acc1
6+
[] 1 then Acc2
7+
else {Fib N-1 Acc2 Acc1+Acc2}
8+
end
9+
end
10+
in
11+
{Fib N 0 1}
12+
end
13+
14+
{Browse {TailFib 32}}
15+
{Browse {TailFib 42}}

0 commit comments

Comments
 (0)