Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Lazy evaluation
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Lazy evaluation
(section)
Add topic