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
Oz (programming language)
(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!
===Functions=== Functions<ref name="Advanced Functional Programming in Oz">{{cite book | author = Leif Grönqvist | title = Advanced Functional Programming in Oz | chapter = Higher Order Functions | url = http://www2.gslt.hum.gu.se/~leifg/gslt/doc/ozfunpaper.ps | access-date = 3 November 2014 | archive-url = https://web.archive.org/web/20160303171453/http://www2.gslt.hum.gu.se/~leifg/gslt/doc/ozfunpaper.ps | archive-date = 3 March 2016 | url-status = dead }}</ref> are first class values, allowing [[Higher-order programming|higher order functional]] programming: <syntaxhighlight lang="erlang"> fun {Fact N} if N =< 0 then 1 else N*{Fact N-1} end end </syntaxhighlight><syntaxhighlight lang="erlang"> fun {Comb N K} {Fact N} div ({Fact K} * {Fact N-K}) % integers can't overflow in Oz (unless no memory is left) end fun {SumList List} case List of nil then 0 [] H|T then H+{SumList T} % pattern matching on lists end end </syntaxhighlight> Functions may be used with both free and bound variables. Free variable values are found using static [[Scope (computer science)|lexical scoping]].<ref name="Scoping"> {{cite journal | author = Robert Gentleman|author2=Ross Ihaka | title = Lexical Scope in Statistical Computing | url= https://www.stat.auckland.ac.nz/~ihaka/downloads/lexical.pdf |journal=Journal of Computational and Graphical Statistics |volume= 9|issue= 3, Systems and Languages |date=Sep 2000|pages=491–508|doi=10.1080/10618600.2000.10474895 }} </ref> ====Higher-order programming==== Functions are like other Oz objects. A function can be passed as an attribute to other functions or can be returned in a function. <syntaxhighlight lang="erlang"> fun {Square N} % A general function N*N end fun {Map F Xs} % F is a function here - higher order programming case Xs of nil then nil [] X|Xr then {F X}|{Map F Xr} end end %usage {Browse {Map Square [1 2 3]}} %browses [1 4 9] </syntaxhighlight> ====Anonymous functions==== Like many other functional languages, Oz supports use of [[anonymous function]]s (i.e. functions which do not have a name) with higher order programming. The symbol $ is used to denote these. In the following, the square function is defined anonymously and passed, causing <code>[1 4 9]</code> to be browsed. <syntaxhighlight lang="erlang"> {Browse {Map fun {$ N} N*N end [1 2 3]}} </syntaxhighlight> Since anonymous functions don't have names, it is not possible to define recursive anonymous functions. ====Procedures==== Functions in Oz are supposed to return a value at the last statement encountered in the body of the function during its execution. In the example below, the function Ret returns 5 if X > 0 and -5 otherwise. <syntaxhighlight lang="erlang"> declare fun {Ret X} if X > 0 then 5 else ~5 end end </syntaxhighlight> But Oz also provides a facility in case a function must not return values. Such functions are called procedures.<ref>{{Cite web|url=https://mozart.github.io/mozart-v1/doc-1.4.0/tutorial/node5.html#control.procedure|title = 5 Basic Control Structures}}</ref> Procedures are defined using the construct "proc" as follows <syntaxhighlight lang="erlang"> declare proc {Ret X} if X > 0 then {Browse 5} else {Browse ~5} end end </syntaxhighlight> The above example doesn't return any value, it just prints 5 or -5 in the Oz browser depending on the sign of X.
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)