Skip to content

Instantly share code, notes, and snippets.

@Tarrasch
Created February 7, 2012 13:51
Show Gist options
  • Select an option

  • Save Tarrasch/1759771 to your computer and use it in GitHub Desktop.

Select an option

Save Tarrasch/1759771 to your computer and use it in GitHub Desktop.
Strict foldr function
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