Created
February 7, 2012 13:51
-
-
Save Tarrasch/1759771 to your computer and use it in GitHub Desktop.
Strict foldr function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module StrictFoldr where | |
| -- A deep strict foldr, very memory inefficient | |
| dsFoldr :: (a -> b -> b) -> b -> [a] -> b | |
| dsFoldr f z [] = z | |
| dsFoldr f z (x:xs) = seq below $ f x $ below | |
| where below = dsFoldr f z xs | |
| -- The lazy value will be `"abc" ++ undefined` whilst the strict will be just `undefined` | |
| valueLazy, valueStrict :: String | |
| valueLazy = foldr (:) undefined "abc" | |
| valueStrict = dsFoldr (:) undefined "abc" | |
| main = do | |
| print $ head valueLazy | |
| print $ head valueStrict |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment