User Tools

Site Tools


codesnippets:folds

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
codesnippets:folds [2021/04/08 17:52] – [Keeping order and beeing fast?] f2b216codesnippets:folds [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 350: Line 350:
     * executes using large heap space     * executes using large heap space
       * {{:codesnippets:heapconsumptionfoldronnoncommutativeop.png?200|}}       * {{:codesnippets:heapconsumptionfoldronnoncommutativeop.png?200|}}
-  * example, using foldl and reverse:<code Haskell>+  * example, using ''foldl'' and ''reverse'':<code Haskell>
 import qualified Data.List as L import qualified Data.List as L
  
Line 376: Line 376:
 </code> </code>
     * executes within approximately __**15 seconds**__ on Intel(R) Core(TM) i5-8400 @ 2.80GHz     * executes within approximately __**15 seconds**__ on Intel(R) Core(TM) i5-8400 @ 2.80GHz
-      * regardless of whether L.foldl' or foldl is used 
     * executes using large heap space     * executes using large heap space
       * {{:codesnippets:heapconsumptionfoldlfronnoncommutativeop.png?200|}}       * {{:codesnippets:heapconsumptionfoldlfronnoncommutativeop.png?200|}}
 +      * regardless of whether ''L.foldl' '' or ''foldl'' is used
 +      * regardless of whether alternative ''reverse'' functions are implemented
 +        * e.g.:<code Haskell>
 +reverse' :: [a] -> [a]
 +reverse' (x:rlx) = reverse'' [x] rlx
 +    where
 +        reverse'' :: [a] -> [a] -> [a]
 +        reverse'' lxReverse [] = lxReverse
 +        reverse'' lxReverse (x:rlx) = reverse'' (x : lxReverse) rlx
 +</code>
 +        * or<code Haskell>
 +reverse' :: [a] -> [a]
 +reverse' l = foldl (flip (:)) [] l
 +</code>
 +        * or<code Haskell>
 +reverse' :: [a] -> [a]
 +reverse' l = L.foldl' (flip (:)) [] l
 +</code>
  
 ====== Conclusion and selection ====== ====== Conclusion and selection ======
Line 389: Line 406:
       * if the initial value is the first and the last respectively use ''foldr1''       * if the initial value is the first and the last respectively use ''foldr1''
  
 +
 +===== ✎ =====
 +~~DISCUSSION~~
codesnippets/folds.1617897169.txt.gz · Last modified: (external edit)

Except where otherwise noted, content on this wiki is licensed under the following license: CC0 1.0 Universal
CC0 1.0 Universal Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki