From: Camil Staps Date: Thu, 12 Mar 2015 12:38:27 +0000 (+0100) Subject: Don't know how to make takeWhile work nicely... X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=635235781923962beb50fb608a3d6e5b35c4cf38;p=fp1415.git Don't know how to make takeWhile work nicely... --- diff --git a/week5/camil/Origami.icl b/week5/camil/Origami.icl index d844d2c..74362a9 100644 --- a/week5/camil/Origami.icl +++ b/week5/camil/Origami.icl @@ -2,21 +2,23 @@ module Origami import StdEnv -/*Start = and +Start = and [ sum` [1 .. 5] == sum [1 .. 5] , prod` [1 .. 5] == prod [1 .. 5] , flatten` [[],[1],[1,2],[1,2,3]] == flatten [[],[1],[1,2],[1,2,3]] , reverse` [1 .. 5] == reverse [1 .. 5] , takeWhile` ((<>) 0) [1,2,3,0,4,5,6] == takeWhile ((<>) 0) [1,2,3,0,4,5,6] , maxList` [1 .. 5] == maxList [1 .. 5] - ]*/ -Start = takeWhile` ((<>) 0) [1,2,3,0,4,5,6] + ] sum` = foldr (+) 0 prod` = foldr (*) 1 flatten` = foldr (++) [] length` = foldl (\l e = l + 1) 0 reverse` = foldl (\xs x = [x:xs]) [] -takeWhile` p = foldr (\x xs = if (p x) [x:xs] xs) [] +takeWhile` _ [] = [] +takeWhile` p xs + | p (xs!!0) = take (maxList [i \\ i <- [0..length xs-1] | foldr (&&) True [p (xs!!j) \\ j <- [0..i]]] + 1) xs + | otherwise = [] maxList` [] = undef maxList` [x:xs] = foldr max x xs