Skip to content
Extraits de code Groupes Projets
Non vérifiée Valider a3d778fa rédigé par Gilles Peiffer's avatar Gilles Peiffer Validation de GitHub
Parcourir les fichiers

Merge pull request #813 from Dj0ulo/master

LINGI1131: Add solutions for APEs 2, 4, and 5
parents b249dbed 558122eb
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
Affichage de
avec 276 ajouts et 105 suppressions
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
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
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
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
%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
%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
%a
local Fib in
fun{Fib N}
if N<2 then 1
else {Fib N-1}+{Fib N-2}
end
end
end
%b
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
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
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
%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
%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
%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
......@@ -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}
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter