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
Higher-order function
(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!
====Erlang==== {{further information|Erlang (programming language)}} <syntaxhighlight lang="erlang"> or_else([], _) -> false; or_else([F | Fs], X) -> or_else(Fs, X, F(X)). or_else(Fs, X, false) -> or_else(Fs, X); or_else(Fs, _, {false, Y}) -> or_else(Fs, Y); or_else(_, _, R) -> R. or_else([fun erlang:is_integer/1, fun erlang:is_atom/1, fun erlang:is_list/1], 3.23). </syntaxhighlight> In this Erlang example, the higher-order function {{code|or_else/2}} takes a list of functions ({{code|Fs}}) and argument ({{code|X}}). It evaluates the function {{code|F}} with the argument {{code|X}} as argument. If the function {{code|F}} returns false then the next function in {{code|Fs}} will be evaluated. If the function {{code|F}} returns {{code|{false, Y} }} then the next function in {{code|Fs}} with argument {{code|Y}} will be evaluated. If the function {{code|F}} returns {{code|R}} the higher-order function {{code|or_else/2}} will return {{code|R}}. Note that {{code|X}}, {{code|Y}}, and {{code|R}} can be functions. The example returns {{code|false}}.
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)