Created
July 18, 2015 06:47
-
-
Save Risto-Stevcev/4662d57269b641a6128d to your computer and use it in GitHub Desktop.
Lazy evaluation
This file contains 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
-- 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