<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 David,</div><div class=""><br class=""></div><div class="">Thanks a lot for the code!</div><div class=""><br class=""></div><div class="">foldr is indeed elegant.</div><div class=""><div class="">In general is it advisable to use auxiliary functions or foldr/foldl variations.</div><div class="">Does it have any performance benefits or ghc would generate same core language for both the functions?</div><div class=""><br class=""></div><div class="">Regards,</div><div class="">Apoorv</div></div><div class=""><br class=""></div><div><blockquote type="cite" class=""><div class="">On Jul 11, 2017, at 15:40, David Ringo <<a href="mailto:davidmringo@gmail.com" class="">davidmringo@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class="">Hi Apoorv,<div class=""><br class=""></div><div class="">There is indeed a left fold:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace" class="">foldlpart :: [Int] -> [a] -> [[a]]<br class=""></font></div><div class=""><font face="monospace" class="">foldlpart ds ps = result</font></div><div class=""><font face="monospace" class=""> where result | null remaining = initial</font></div><div class=""><font face="monospace" class=""> | otherwise = initial ++ [remaining]</font></div><div class=""><font face="monospace" class=""> (initial, remaining) = foldl aux ([], ps) ds</font></div><div class=""><font face="monospace" class=""> aux (l, xs) d = case xs of</font></div><div class=""><font face="monospace" class=""> [] -> (l, xs)</font></div><div class=""><font face="monospace" class=""> _ -> let (f,s) = splitAt d xs in (l ++ [f], s)</font></div><div class=""><br class=""></div><div class="">I'm sure someone else can put something better together though.</div><div class=""><br class=""></div><div class="">I much prefer this right fold, since it avoids quadratic behavior incurred with (++) above:</div><div class=""><br class=""></div><div class=""><div class=""><font face="monospace" class="">foldrpart :: [Int] -> [a] -> [[a]]</font></div><div class=""><font face="monospace" class="">foldrpart ds ps = myFunc ps</font></div><div class=""><font face="monospace" class=""> where myFunc = foldr buildMyFunc (: []) ds</font></div><div class=""><font face="monospace" class=""> buildMyFunc digit func = \ps -></font></div><div class=""><font face="monospace" class=""> case ps of</font></div><div class=""><font face="monospace" class=""> [] -> []</font></div><div class=""><font face="monospace" class=""> _ -> let (first, last) = splitAt digit ps</font></div><div class=""><font face="monospace" class=""> in first : func last</font></div></div><div class=""><br class=""></div><div class="">If it's unclear, buildMyFunc is basically composing a bunch of functions which know (from the fold on the list of Ints) how many elements</div><div class="">to take from some list.</div><div class=""><br class=""></div><div class="">Hope this is useful.</div><div class=""><br class=""></div><div class="">- David</div><div class=""><br class=""><div class="gmail_quote"><div dir="ltr" class="">On Tue, Jul 11, 2017 at 3:30 PM Apoorv Ingle <<a href="mailto:apoorv.ingle@gmail.com" class="">apoorv.ingle@gmail.com</a>> wrote:<br class=""></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word" 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 style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><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 style="color:rgb(0,86,214)" class="">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 style="color:rgb(0,86,214)" class="">paux</span><font color="#0056d6" class=""> [] [] ps' = ps'</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span style="color:rgb(0,86,214)" class="">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 style="color:rgb(0,86,214)" class="">paux</span><font color="#0056d6" class=""> _ [] ps' = ps'</font></div></div></div><div class=""><div class=""><div class=""><font color="#0056d6" class=""> </font><span style="color:rgb(0,86,214)" class="">paux</span><font color="#0056d6" class=""> (d:ds') ps ps' = </font><span style="color:rgb(0,86,214)" class="">paux</span><font color="#0056d6" class=""> ds' (snd (splitAt d ps)) ([fst (split</font><span style="color:rgb(0,86,214)" class="">At d ps)] ++ ps')</span></div></div></div><div class=""><font color="#0056d6" class=""><br class=""></font></div></blockquote>——————<br class=""><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><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 style="margin:0px 0px 0px 40px;border:none;padding:0px" class=""><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 style="word-wrap:break-word" class="">Apoorv Ingle<br class="">Graduate Student, Computer Science<br class=""><a href="mailto:apoorv.ingle@ku.edu" target="_blank" class="">apoorv.ingle@ku.edu</a></div></div></div>_______________________________________________<br class="">
Beginners mailing list<br class="">
<a href="mailto:Beginners@haskell.org" target="_blank" class="">Beginners@haskell.org</a><br class="">
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank" class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br class="">
</blockquote></div></div></div></div>
_______________________________________________<br class="">Beginners mailing list<br class=""><a href="mailto:Beginners@haskell.org" class="">Beginners@haskell.org</a><br class="">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners<br class=""></div></blockquote></div><br class=""></body></html>