Skip to content

Instantly share code, notes, and snippets.

@Risto-Stevcev
Created July 18, 2015 06:47
Show Gist options
  • Save Risto-Stevcev/4662d57269b641a6128d to your computer and use it in GitHub Desktop.
Save Risto-Stevcev/4662d57269b641a6128d to your computer and use it in GitHub Desktop.
Lazy evaluation
-- In GHCi
:set +m
let x y = x y
-- ex: x 2 will loop infinitely
:{
let {
z y = y
where
t = x 2
}
:}
-- ex: t = x 2 *would* loop infinitely if strictly evaluated, but doesn't because of lazy evaluation
z 2
-- returns 2
z 7
-- returns 7
-- the expression t = x 2 becomes a thunk and introduces space complexity to the function
-- lazy evaluation in haskell can make code more elegant, but otherwise make it difficult to evaluate the time and (particularly the) space complexity of your programs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment