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
Conditional (computer programming)
(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!
=== Lambda calculus === In [[Lambda calculus]], the concept of an if-then-else conditional can be expressed using the following expressions: true = 位x. 位y. x false = 位x. 位y. y ifThenElse = (位c. 位x. 位y. (c x y)) # true takes up to two arguments and once both are provided (see [[currying]]), it returns the first argument given. # false takes up to two arguments and once both are provided(see [[currying]]), it returns the second argument given. # ifThenElse takes up to three arguments and once all are provided, it passes both second and third argument to the first argument(which is a function that given two arguments, and produces a result). We expect ifThenElse to only take true or false as an argument, both of which project the given two arguments to their preferred single argument, which is then returned. '''note''': if ifThenElse is passed two functions as the left and right conditionals; it is '''necessary''' to also pass an empty tuple () to the result of ifThenElse in order to actually call the chosen function, otherwise ifThenElse will just return the function object without getting called. In a system where numbers can be used without definition (like Lisp, Traditional paper math, so on), the above can be expressed as a single closure below: <syntaxhighlight lang="lisp"> ((位true. 位false. 位ifThenElse. (ifThenElse true 2 3) )(位x. 位y. x)(位x. 位y. y)(位c. 位l. 位r. c l r)) </syntaxhighlight> Here, true, false, and ifThenElse are bound to their respective definitions which are passed to their scope at the end of their block. A working JavaScript analogy(using only functions of single variable for rigor) to this is as follows: <syntaxhighlight lang="JavaScript"> var computationResult = ((_true => _false => _ifThenElse => _ifThenElse(_true)(2)(3) )(x => y => x)(x => y => y)(c => x => y => c(x)(y))); </syntaxhighlight> The code above with multivariable functions looks like this: <syntaxhighlight lang="JavaScript"> var computationResult = ((_true, _false, _ifThenElse) => _ifThenElse(_true, 2, 3) )((x, y) => x, (x, y) => y, (c, x, y) => c(x, y)); </syntaxhighlight> Another version of the earlier example without a system where numbers are assumed is below. The first example shows the first branch being taken, while second example shows the second branch being taken. <syntaxhighlight lang="lisp"> ((位true. 位false. 位ifThenElse. (ifThenElse true (位FirstBranch. FirstBranch) (位SecondBranch. SecondBranch)) )(位x. 位y. x)(位x. 位y. y)(位c. 位l. 位r. c l r)) ((位true. 位false. 位ifThenElse. (ifThenElse false (位FirstBranch. FirstBranch) (位SecondBranch. SecondBranch)) )(位x. 位y. x)(位x. 位y. y)(位c. 位l. 位r. c l r)) </syntaxhighlight> Smalltalk uses a similar idea for its true and false representations, with True and False being singleton objects that respond to messages ifTrue/ifFalse differently. Haskell used to use this exact model for its Boolean type, but at the time of writing, most Haskell programs use syntactic sugar "if a then b else c" construct which unlike ifThenElse does not compose unless either wrapped in another function or re-implemented as shown in The Haskell section of this page.
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)