<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">Hi,</div><div class=""><br class=""></div><div class="">I am trying to write a partition function where we pass group sizes and the list we want to partition into groups </div><div class="">as arguments and get back a list of groups (or list of lists in this case). My first attempt was by using an auxiliary inner function</div><div class=""><br class=""></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class=""><font color="#0056d6" class="">{-# LANGUAGE ScopedTypeVariables #-}</font></div></div><div class=""><div class=""><font color="#0056d6" class=""><br class=""></font></div></div><div class=""><div class=""><font color="#0056d6" class="">module Partition where</font></div></div><div class=""><font color="#0056d6" class=""><br class=""></font></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">partition :: [Int] -> [a] -> [[a]]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">partition ds ps = reverse $ paux ds ps []</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> where</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span class="" style="color: rgb(0, 86, 214);">paux</span><font color="#0056d6" class=""> :: [Int] -> [a] -> [[a]] -> [[a]]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span class="" style="color: rgb(0, 86, 214);">paux</span><font color="#0056d6" class=""> [] [] ps' = ps'</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span class="" style="color: rgb(0, 86, 214);">paux</span><font color="#0056d6" class=""> [] ps ps' = [ps] ++ ps’ </font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span class="" style="color: rgb(0, 86, 214);">paux</span><font color="#0056d6" class=""> _ [] ps' = ps'</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span class="" style="color: rgb(0, 86, 214);">paux</span><font color="#0056d6" class=""> (d:ds') ps ps' = </font><span class="" style="color: rgb(0, 86, 214);">paux</span><font color="#0056d6" class=""> ds' (snd (splitAt d ps)) ([fst (split</font><span class="" style="color: rgb(0, 86, 214);">At d ps)] ++ ps')</span></div></div></div><div class=""><font color="#0056d6" class=""><br class=""></font></div></blockquote>——————<br class=""><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><font color="#0056d6" class=""><br class=""></font></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">*Partition> partition [2, 3] [1,2,3,4,5]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">[[1,2],[3,4,5]]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">*Partition> partition [1, 2] [1,2,3,4,5]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">[[1],[2,3],[4,5]]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">*Partition> partition [1, 2, 5] [1,2,3,4,5]</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class="">[[1],[2,3],[4,5]]</font></div></div></div></blockquote><div class=""><div class=""><br class=""></div><div class=""><br class=""></div><div class="">I was speculating if we could write the same function using foldl function but haven’t been able to figure it out.</div><div class="">I would really appreciate if you can give me pointers on how we can implement it.</div><div class=""><br class=""></div></div><blockquote class="" style="margin: 0px 0px 0px 40px; border: none; padding: 0px;"><div class=""><div class=""><font color="#0056d6" class="">partition' :: [Int] -> [a] -> [[a]]</font></div></div><div class=""><div class=""><font color="#0056d6" class="">partition' [] ds = [ds]</font></div></div><div class=""><div class=""><font color="#0056d6" class="">partition' ps ds = foldl ??? ???' ???''</font></div></div></blockquote><div class=""><br class=""></div><div class="">contrary to my speculation is it even possible to write such a function using foldl if so why not?</div><div class=""><br class=""></div>Regards,<br class=""><div class=""><div class="" style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">Apoorv Ingle<br class="">Graduate Student, Computer Science<br class=""><a href="mailto:apoorv.ingle@ku.edu" class="">apoorv.ingle@ku.edu</a></div></div></body></html>