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:43] f2b216codesnippets:folds [2025/10/08 00:48] (current) – external edit 127.0.0.1
Line 327: Line 327:
     * large lists     * large lists
     * order of operations to be kept as is foldr     * order of operations to be kept as is foldr
-    * executed with small impact on heap space 
-    * fast to be executed, almost as foldl does 
- 
-The implications regarding order of execution: 
   * example, using foldr:<code Haskell>   * example, using foldr:<code Haskell>
 main :: IO () main :: IO ()
Line 342: Line 338:
 lnNumbers2 = [31,13,3] lnNumbers2 = [31,13,3]
 lnNumbers3 = [1..100000000] lnNumbers3 = [1..100000000]
- +
 f1 :: [Integer] -> Integer f1 :: [Integer] -> Integer
-f1 ln = foldr (-) ln+f1 ln = foldr (-) ln
 </code> </code>
     * executes, with output:<code>     * executes, with output:<code>
Line 351: Line 347:
 -50000000 -50000000
 </code> </code>
-  * example, using foldr:<code Haskell>+    * executes within approximately __**15 seconds**__ on Intel(R) Core(TM) i5-8400 @ 2.80GHz 
 +    * executes using large heap space 
 +      * {{:codesnippets:heapconsumptionfoldronnoncommutativeop.png?200|}} 
 +  * example, using ''foldl'' and ''reverse'':<code Haskell>
 import qualified Data.List as L import qualified Data.List as L
  
Line 375: Line 374:
 21 21
 -50000000 -50000000
 +</code>
 +    * executes within approximately __**15 seconds**__ on Intel(R) Core(TM) i5-8400 @ 2.80GHz
 +    * executes using large heap space
 +      * {{: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> </code>
  
Line 386: 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.1617896624.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