diff --git a/src/q6/oz-INGI1131/exercises/APE2/q1.oz b/src/q6/oz-INGI1131/exercises/APE2/q1.oz new file mode 100644 index 0000000000000000000000000000000000000000..a4c90d1d127e8f1663a27c08b526643c91fb5efb --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q1.oz @@ -0,0 +1,15 @@ +declare +proc {Max L Z} + proc {MaxLoop L M R} + case L of nil then R = M + [] H|T then + if M > H then {MaxLoop T M R} + else {MaxLoop T H R} end + end + end +in + if L == nil then Z = error + else {MaxLoop L.2 L.1 Z} end +end + +{Browse {Max [4 2 7 8 6]}} \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE2/q10.oz b/src/q6/oz-INGI1131/exercises/APE2/q10.oz new file mode 100644 index 0000000000000000000000000000000000000000..ae20b0c2442484cdbbc283d103399e120cceed6e --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q10.oz @@ -0,0 +1,40 @@ +local + fun {Flatten X} + fun {DoFlatten Xs Start End} + case Xs + of X|Xr then S S1 in + if {DoFlatten X S S1} then + S = Start + {DoFlatten Xr S1 End} + else S2 in + Start = X|S2 + {DoFlatten Xr S2 End} + end + [] nil then + Start = End + true + else false + end + end + Start + in + if {DoFlatten X Start nil} then Start + else X + end + end +in + {Browse {Flatten [a [b [c d]] e [[[f]]]]}} % -> [a b c d e f] +end + +%%% Simpler, but less optimized version %%% +local + fun {Flatten X} + case X + of H|T then {Append {Flatten H} {Flatten T}} + [] nil then nil + else [X] + end + end +in + {Browse {Flatten [a [b [c d]] e [[[f]]]]}} % -> [a b c d e f] +end diff --git a/src/q6/oz-INGI1131/exercises/APE2/q11.oz b/src/q6/oz-INGI1131/exercises/APE2/q11.oz new file mode 100644 index 0000000000000000000000000000000000000000..9f423722d9a7391b8c41d0e48d9c8689b5c8644f --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q11.oz @@ -0,0 +1,11 @@ +declare +proc{Fact N R} + if N==1 then R = [N] + else + Out = {Fact N-1} + in + R = N*Out.1|Out + end +end + +{Browse {Fact 4}} %-> [24 6 2 1] \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE2/q12.oz b/src/q6/oz-INGI1131/exercises/APE2/q12.oz new file mode 100644 index 0000000000000000000000000000000000000000..4cd09ccdcc8dd18fe8a24d01937550382163e7b5 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q12.oz @@ -0,0 +1,19 @@ +declare +fun {DictionaryFilter D F} + case D + of leaf then nil + [] dict(key:Key info:Info left:Left right:Right) then + DicL = {DictionaryFilter Left F} + DicR = {DictionaryFilter Right F} + in + if({F Info})then + {Append DicL Key#Info|DicR} + else + {Append DicL DicR} + end + end +end + +Class=dict(key:10 info:person('Christian'19) left:dict(key:7 info:person('Denys'25) left:leaf right:dict(key:9 info:person('David'7) left:leaf right:leaf)) right:dict(key:18 info:person('Rose'12) left:dict(key:14 info:person('Ann'27) left:leaf right:leaf) right:leaf)) + +{Browse {DictionaryFilter Class fun {$ Info}Info.2>20 end}} %->[7#person('Denys' 25) 14#person('Ann' 27)] \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE2/q2.oz b/src/q6/oz-INGI1131/exercises/APE2/q2.oz new file mode 100644 index 0000000000000000000000000000000000000000..1940540ba5b12b31c967787ca96fcad27ba54396 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q2.oz @@ -0,0 +1,12 @@ +%a +declare +fun {Sum N M} + if N == 0 then M + else {Sum N-1 M+N} + end +end + +{Browse {Sum 8 0}} +%b + +%c diff --git a/src/q6/oz-INGI1131/exercises/APE2/q4.oz b/src/q6/oz-INGI1131/exercises/APE2/q4.oz new file mode 100644 index 0000000000000000000000000000000000000000..a508a76b90d1ca9e7532ca6213d61d9c4206e9b0 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q4.oz @@ -0,0 +1,33 @@ +%a +define +fun {ForAllTail Xs P} + case Xs of nil then skip + [] H|T then + {P Xs} + {ForAllTail T P} +end + +%b +fun {GetElementsInOrder Tree} + if Tree.left == nil && Tree.right == nil then [Tree.info] + elseif Tree.left == nil then Tree.info|{GetElementsInOrder Tree.right} + elseif Tree.right == nil then Append{{GetElementsInOrder Tree.left} [Tree.info]} + else Append{Append{{GetElementsInOrder Tree.left} [Tree.info]} {GetElementsInOrder Tree.right} + end +end + +%(plus court) +fun {GetElementsInOrder Tree} + fun{Loop T Li} + case T + of nil then Li + [] tree(info:I left:L right:R) then + {Loop R {Append {Loop L Li} [I]}} + end + end +in + {Loop Tree nil} +end + +Tree = tree(info:10 left:tree(info:7 left:nil right:tree(info:9 left:nil right:nil)) right:tree(info:18 left:tree(info:14 left:nil right:nil) right:nil)) +{Browse {GetElementsInOrder Tree}} %-> [7 9 10 14 18] \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE2/q5.oz b/src/q6/oz-INGI1131/exercises/APE2/q5.oz new file mode 100644 index 0000000000000000000000000000000000000000..22e95c7f2b1823030e6faa1c268a425d1ff37c3f --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q5.oz @@ -0,0 +1,10 @@ +%a +local Fib in + fun{Fib N} + if N<2 then 1 + else {Fib N-1}+{Fib N-2} + end + end +end + +%b diff --git a/src/q6/oz-INGI1131/exercises/APE2/q6.oz b/src/q6/oz-INGI1131/exercises/APE2/q6.oz new file mode 100644 index 0000000000000000000000000000000000000000..d3dc9f6d3a2f7ca37419072a7fdc6b5151f126c7 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q6.oz @@ -0,0 +1,32 @@ +declare +fun {Add A B} + fun {AddDigits A B C} + SUM = A+B+C + OUT = output(sum:_ carry:_) + in + case SUM + of 0 then OUT.sum=0 OUT.carry=0 + [] 1 then OUT.sum=1 OUT.carry=0 + [] 2 then OUT.sum=0 OUT.carry=1 + [] 3 then OUT.sum=1 OUT.carry=1 + end + OUT + end + + fun {Loop A B} + Prev + Addition + in + if A==nil then output(r:nil carry:0) + else + Prev = {Loop A.2 B.2} + Addition = {AddDigits A.1 B.1 Prev.carry} + output(r:Addition.sum|Prev.r carry:Addition.carry) + end + end + Addition = {Loop A B} +in + Addition.carry|Addition.r +end + +{Browse {Add [1 1 0 1 1 0] [0 1 0 1 1 1]}} % -> [1 0 0 1 1 0 1] \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE2/q7-8-9.oz b/src/q6/oz-INGI1131/exercises/APE2/q7-8-9.oz new file mode 100644 index 0000000000000000000000000000000000000000..a2c7d2ab21a2f7834cd1a3601bda9549cbe97c49 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE2/q7-8-9.oz @@ -0,0 +1,17 @@ +declare +%7 +fun {Filter L F} + if L==nil then nil + elseif {F L.1} then L.1|{Filter L.2 F} + else {Filter L.2 F} + end +end + +%8 +fun {IsEven A} + A mod 2 == 0 +end + +{Browse {Filter [1 2 3 4 6 7] IsEven}} + +%9 diff --git a/src/q6/oz-INGI1131/exercises/APE4/q5.oz b/src/q6/oz-INGI1131/exercises/APE4/q5.oz new file mode 100644 index 0000000000000000000000000000000000000000..30875cd5309058769267dffe8ad9ed55d2aa95d9 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE4/q5.oz @@ -0,0 +1,24 @@ +declare +proc{MapRecord Done R1 F R2} + A={Record.arity R1} + proc{Loop L X} + case L of nil then X=unit + [] H|T then + XTail + in + thread R2.H={F R1.H}end + {Loop T XTail} + {Wait R2.H} + {Wait XTail} + X=unit + end + end +in + R2={Record.make {Record.label R1} A} + {Loop A Done} +end + +Done +R = {MapRecord Done '#'(a:1 b:2 c:3 d:4 e:5 f:6 g:7) fun {$ X} {Delay 1000} 2*X end} +{Wait Done} +{Show R} \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE5/q3.oz b/src/q6/oz-INGI1131/exercises/APE5/q3.oz deleted file mode 100644 index 1ff18eb4da843f92acb7ea0ed70ee58c9340f6db..0000000000000000000000000000000000000000 --- a/src/q6/oz-INGI1131/exercises/APE5/q3.oz +++ /dev/null @@ -1,10 +0,0 @@ -%Exo 3 -declare -L1 L2 F -L1 = [1 2 3] -F = fun {$ X} {Delay 200} X*X end -thread L2 = {Map L1 F} end -{Wait L2} -{Show L2} - -%TODO \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE5/q3a.oz b/src/q6/oz-INGI1131/exercises/APE5/q3a.oz new file mode 100644 index 0000000000000000000000000000000000000000..6c06f522b89dee59e91fdfa34a50a025284d59f6 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE5/q3a.oz @@ -0,0 +1,9 @@ +%a +declare +L1 L2 F +L1 = [1 2 3] +F = fun{$ X} {Delay 200} X*X end +X +thread L2 = {Map L1 F} X=unit end +{Wait X} +{Show L2} \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/APE5/q3b.oz b/src/q6/oz-INGI1131/exercises/APE5/q3b.oz new file mode 100644 index 0000000000000000000000000000000000000000..421309d28b87ce44615e2cfa74eb8edd66a051e2 --- /dev/null +++ b/src/q6/oz-INGI1131/exercises/APE5/q3b.oz @@ -0,0 +1,10 @@ +%b +declare +L1 L2 L3 L4 +X1 X2 X3 +L1= [1 2 3] +thread L2={Map L1 fun{$ X} {Delay 200} X*X end} X1=unit end +thread L3={Map L1 fun{$ X} {Delay 200} 2*X end} X2=unit end +thread L4={Map L1 fun{$ X} {Delay 200} 3*X end} X3=unit end +{Wait X1} {Wait X2} {Wait X3} +{Show L2#L3#L4} \ No newline at end of file diff --git a/src/q6/oz-INGI1131/exercises/oz-INGI1131-exercises.tex b/src/q6/oz-INGI1131/exercises/oz-INGI1131-exercises.tex index 6b5813d521ff6a218f44b059ccc3264d9fe8c219..1990b3de97a74d1be649f38be48927693f4d14a5 100644 --- a/src/q6/oz-INGI1131/exercises/oz-INGI1131-exercises.tex +++ b/src/q6/oz-INGI1131/exercises/oz-INGI1131-exercises.tex @@ -284,113 +284,57 @@ local X Y Z in end \end{lstlisting} -\section{Procedures, Functions and Tail Recursion} -\subsection{Functions are procedures} -\begin{lstlisting} -declare -proc {Max L Z} - proc {MaxLoop L M R} - case L of nil then R = M - [] H|T then - if M > H then {MaxLoop T M R} - else {MaxLoop T H R} end - end - end -in - if L == nil then Z = error - else {MaxLoop L.2 L.1 Z} end -end - -\end{lstlisting} +\section{Procedures, Functions and Tail Recursion}%APE2 +\subsection{Functions are procedures} +\begin{solution} + \lstinputlisting{APE2/q1.oz} +\end{solution} \subsection{Tail recursion} +\begin{solution} + \lstinputlisting{APE2/q2.oz} +\end{solution} +\subsection{Arithmetic operations and data structures} +\nosolution -\begin{lstlisting} -%a -fun {Sum N M} -if N == 0 then M -else {Sum N-1 M+N} -end -end -{Browse {Sum 8 0}} - - -%b -Ã completer https://github.com/gp2mv3/Syntheses - - -%c - -\end{lstlisting} - -\subsection{Recursive functions on lists and tress} - -\begin{lstlisting} -%a -define -fun {ForAllTail Xs P} - case Xs of nil then skip - [] H|T then - {P Xs} - {ForAllTail T P} -end - -%b - fun {GetElementsInOrder Tree} - if Tree.left == nil && Tree.right == nil then [Tree.info] - elseif Tree.left == nil then Tree.info|{GetElementsInOrder Tree.right} - elseif Tree.right == nil then Append{{GetElementsInOrder Tree.left} [Tree.info]} - else Append{Append{{GetElementsInOrder Tree.left} [Tree.info]} {GetElementsInOrder Tree.right} - end - - end - -\end{lstlisting} +\subsection{Recursive functions on lists and trees} +\begin{solution} + \lstinputlisting{APE2/q4.oz} +\end{solution} \subsection{Fibonacci function} +\begin{solution} + \lstinputlisting{APE2/q5.oz} +\end{solution} -\begin{lstlisting} -%a -local Fib in - fun{Fib N} - if N<2 then 1 - else {Fib N-1}+{Fib N-2} - end - - end -end - -%b -%pas fait - - -\end{lstlisting} +\subsection{Add binary numbers function} +\begin{solution} + \lstinputlisting{APE2/q6.oz} +\end{solution} \subsection{High-order functions} +\begin{solution} + \lstinputlisting{APE2/q7-8-9.oz} +\end{solution} -\begin{lstlisting} -%7 - fun {Filter L F} - if L==nil then nil - elseif {F L.1} then L.1|{Filter L.2 F} - else {Filter L.2 F} - end - - - end - +\subsection{Flatten list function} +\begin{solution} + \lstinputlisting{APE2/q10.oz} +\end{solution} -%8 - fun {Pair A} - A mod 2 == 0 +\subsection{Fact procedure} +\begin{solution} + \lstinputlisting{APE2/q11.oz} +\end{solution} - end - {Browse {Filter [1 2 3 4 6 7] Pair}} +\subsection{DictionaryFilter function} +\begin{solution} + \lstinputlisting{APE2/q12.oz} +\end{solution} -\end{lstlisting} \clearpage \section{Threads and declarative concurrency}%3 @@ -450,8 +394,10 @@ Useless \begin{solution} \lstinputlisting{APE4/q4.oz} \end{solution} -\subsection{Records and concurrency} -\nosolution +\subsection{MapRecord}\label{MapRecordConcurrent} +\begin{solution} + \lstinputlisting{APE4/q5.oz} +\end{solution} \clearpage @@ -468,9 +414,12 @@ Useless \subsection{Thread termination} \begin{solution} - \lstinputlisting{APE5/q3.oz} + \lstinputlisting{APE5/q3a.oz} + \lstinputlisting{APE5/q3b.oz} + MapRecord (see \ref{MapRecordConcurrent}) \end{solution} + \clearpage \section{Laziness}%6 \subsection{Lazy generation, Touch and GiveMeNth}