From 38271f0d2fba7cf30d16c74407a89bc18fc37c1f Mon Sep 17 00:00:00 2001
From: Dj0ulo <julien.launois@student.uclouvain.be>
Date: Sun, 26 Apr 2020 18:51:44 +0200
Subject: [PATCH] =?UTF-8?q?INGI1131=20-=20APE=202=20-=20q10=20optimis?=
 =?UTF-8?q?=C3=A9e?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/q6/oz-INGI1131/exercises/APE2/q10.oz | 29 ++++++++++++++++++------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/q6/oz-INGI1131/exercises/APE2/q10.oz b/src/q6/oz-INGI1131/exercises/APE2/q10.oz
index 0cf8ce640..38a3826f5 100644
--- a/src/q6/oz-INGI1131/exercises/APE2/q10.oz
+++ b/src/q6/oz-INGI1131/exercises/APE2/q10.oz
@@ -1,10 +1,25 @@
 declare
-fun {Flatten List}
-  case List
-  of nil then nil
-  [] H|T then {Append {Flatten H} {Flatten T}}
-  else [List]    
-  end
+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
-
 {Browse {Flatten [a [b [c d]] e [[[f]]]]}} % -> [a b c d e f]
\ No newline at end of file
-- 
GitLab