Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Lazy evaluation
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Control structures=== {{More citations needed section|date=March 2011}} Lazy evaluation allows control structures to be defined normally, and not as primitives or compile-time techniques. For example, one can define [[if-then-else]] and [[short-circuit evaluation]] operators:<ref>{{cite web |title=utility-ht: Data.Bool.HT.Private |url=https://hackage.haskell.org/package/utility-ht-0.0.16/docs/src/Data.Bool.HT.Private.html#if%27 |website=hackage.haskell.org |access-date=8 January 2022}}</ref><ref>{{cite web |title=The Haskell 98 Report: Standard Prelude |url=https://www.haskell.org/onlinereport/standard-prelude.html |website=www.haskell.org |access-date=8 January 2022 |location=Boolean functions}}</ref> <syntaxhighlight lang="haskell"> ifThenElse True b c = b ifThenElse False b c = c -- or True || b = True False || b = b -- and True && b = b False && b = False </syntaxhighlight> These have the usual semantics, i.e., {{code|ifThenElse a b c|lang=Haskell}} evaluates (a), then if and only if (a) evaluates to true does it evaluate (b), otherwise it evaluates (c). That is, exactly one of (b) or (c) will be evaluated. Similarly, for {{code|EasilyComputed <nowiki>||</nowiki> LotsOfWork|lang=Haskell}}, if the easy part gives '''True''' the lots of work expression could be avoided. Finally, when evaluating {{code|SafeToTry && Expression|lang=haskell}}, if ''SafeToTry'' is '''false''' there will be no attempt at evaluating the ''Expression''. Conversely, in an eager language the above definition for {{code|ifThenElse a b c|lang=Haskell}} would evaluate (a), (b), and (c) regardless of the value of (a). This is not the desired behavior, as (b) or (c) may have [[Side effect (computer science)|side effects]], take a long time to compute, or throw errors. It is usually possible to introduce user-defined lazy control structures in eager languages as functions, though they may depart from the language's syntax for eager evaluation: Often the involved code bodies need to be wrapped in a function value, so that they are executed only when called.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)