Don't know how to make takeWhile work nicely...
authorCamil Staps <info@camilstaps.nl>
Thu, 12 Mar 2015 12:38:27 +0000 (13:38 +0100)
committerCamil Staps <info@camilstaps.nl>
Thu, 12 Mar 2015 12:38:27 +0000 (13:38 +0100)
week5/camil/Origami.icl

index d844d2c..74362a9 100644 (file)
@@ -2,21 +2,23 @@ module Origami
 \r
 import StdEnv\r
 \r
-/*Start                        = and\r
+Start                  = and\r
                                  [ sum`       [1 .. 5]                 == sum       [1 .. 5]\r
                                  , prod`      [1 .. 5]                 == prod      [1 .. 5]\r
                                  , flatten`   [[],[1],[1,2],[1,2,3]]   == flatten   [[],[1],[1,2],[1,2,3]]\r
                                  , reverse`   [1 .. 5]                 == reverse   [1 .. 5]\r
                                  , takeWhile` ((<>) 0) [1,2,3,0,4,5,6] == takeWhile ((<>) 0) [1,2,3,0,4,5,6]\r
                                  , maxList`   [1 .. 5]                 == maxList   [1 .. 5]\r
-                                 ]*/\r
-Start = takeWhile` ((<>) 0) [1,2,3,0,4,5,6]\r
+                                 ]\r
 \r
 sum`                   = foldr (+) 0\r
 prod`                  = foldr (*) 1\r
 flatten`               = foldr (++) []\r
 length`                        = foldl (\l e = l + 1) 0\r
 reverse`               = foldl (\xs x = [x:xs]) []\r
-takeWhile` p           = foldr (\x xs = if (p x) [x:xs] xs) []\r
+takeWhile` _ []        = []\r
+takeWhile` p xs\r
+       | p (xs!!0)     = take (maxList [i \\ i <- [0..length xs-1] | foldr (&&) True [p (xs!!j) \\ j <- [0..i]]] + 1) xs\r
+       | otherwise     = []\r
 maxList` []            = undef\r
 maxList` [x:xs]                = foldr max x xs\r