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
Miranda (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!
== Overview == Miranda is a [[lazy evaluation|lazy]], [[functional programming|purely functional]] programming language. That is, it lacks [[Side effect (computer science)|side effect]]s and [[imperative programming]] features. A Miranda program (called a ''script'') is a set of [[equation]]s that define various mathematical [[function (mathematics)|function]]s and [[algebraic data type]]s. The word ''[[Set (mathematics)|set]]'' is important here: the order of the equations is, in general, irrelevant, and there is no need to define an entity prior to its use. Since the [[parsing]] algorithm makes intelligent use of layout (indentation, via [[off-side rule]]), bracketing statements are rarely needed and statement terminators are unneeded. This feature, inspired by [[ISWIM]], is also used in [[occam (programming language)|occam]] and [[Haskell]] and was later popularized by [[Python (programming language)|Python]]. [[Comment (computer programming)|Comment]]ary is introduced into regular scripts by the characters <code>||</code> and continue to the end of the same line. An alternative commenting convention affects an entire source code file, known as a "[[Literate programming|literate script]]", in which every line is considered a comment unless it starts with a <code>></code> sign. Miranda's basic [[data type]]s are <code>char</code>, <code>num</code> and <code>bool</code>. A character string is simply a list of <code>char</code>, while <code>num</code> is silently converted between two underlying forms: [[arbitrary-precision arithmetic|arbitrary-precision]] integers (a.k.a. bignums) by default, and regular [[floating point]] values as required. [[Tuple]]s are sequences of elements of potentially mixed types, analogous to [[record (computer science)|record]]s in [[Pascal (programming language)|Pascal]]-like languages, and are written delimited with parentheses: <syntaxhighlight lang="haskell"> this_employee = ("Folland, Mary", 10560, False, 35) </syntaxhighlight> The ''[[List (abstract data type)|list]]'' instead is the most commonly used data structure in Miranda. It is written delimited by square brackets and with comma-separated elements, all of which must be of the same type: <syntaxhighlight lang="haskell"> week_days = ["Mon","Tue","Wed","Thur","Fri"] </syntaxhighlight> List concatenation is <code>++</code>, subtraction is <code>--</code>, construction is <code>:</code>, sizing is <code>#</code> and indexing is <code>!</code>, so: <syntaxhighlight lang="clean"> days = week_days ++ ["Sat","Sun"] days = "Nil":days days!0 β "Nil" days = days -- ["Nil"] #days β 7 </syntaxhighlight> There are several list-building shortcuts: <code>..</code> is used for lists whose elements form an arithmetic series, with the possibility for specifying an increment other than 1: <syntaxhighlight lang="haskell"> fac n = product [1..n] odd_sum = sum [1,3..100] </syntaxhighlight> More general and powerful list-building facilities are provided by "[[list comprehension]]s" (previously known as "ZF expressions"), which come in two main forms: an expression applied to a series of terms, e.g.: <syntaxhighlight lang="haskell"> squares = [ n * n | n <- [1..] ] </syntaxhighlight> (which is read: list of n squared where n is taken from the list of all positive integers) and a series where each term is a function of the previous one, e.g.: <syntaxhighlight lang="haskell"> powers_of_2 = [ n | n <- 1, 2*n .. ] </syntaxhighlight> As these two examples imply, Miranda allows for lists with an infinite number of elements, of which the simplest is the list of all positive integers: <code>[1..]</code> The notation for function application is simply juxtaposition, as in <code>sin x</code>. In Miranda, as in most other purely functional languages, functions are [[first-class function|first-class]] citizens, which is to say that they can be passed as [[parameter (computer science)|arguments]] to other functions, returned as results, or included as elements of data structures. What is more, a function with two or more parameters may be "partially parameterised", or [[currying|curried]], by supplying fewer arguments than the full number of parameters. This gives another function which, given the remaining parameters, will return a result. For example: <syntaxhighlight lang="haskell"> add a b = a + b increment = add 1 </syntaxhighlight> is a roundabout way of creating a function "increment" which adds one to its argument. In reality, <code>add 4 7</code> takes the two-parameter function <code>add</code>, applies it to <code>4</code> obtaining a single-parameter function that adds four to its argument, then applies that to <code>7</code>. Any function with two parameters (operands) can be turned into an infix operator (for example, given the definition of the <code>add</code> function above, the term <code>$add</code> is in every way equivalent to the <code>+</code> operator) and every infix operator taking two parameters can be turned into a corresponding function. Thus: <syntaxhighlight lang="haskell"> increment = (+) 1 </syntaxhighlight> is the briefest way to create a function that adds one to its argument. Similarly, in <syntaxhighlight lang="haskell"> half = (/ 2) reciprocal = (1 /) </syntaxhighlight> two single-parameter functions are generated. The interpreter understands in each case which of the divide operator's two parameters is being supplied, giving functions which respectively divide a number by two and return its reciprocal. Although Miranda is a [[strongly typed programming language]], it does not insist on explicit type [[declaration (computer science)|declaration]]s. If a function's type is not explicitly declared, the interpreter [[type inference|infer]]s it from the type of its parameters and how they are used within the function. In addition to the basic types (<code>char</code>, <code>num</code>, <code>bool</code>), it includes an "anything" type where the type of a parameter does not matter, as in the list-reversing function: <syntaxhighlight lang="haskell"> rev [] = [] rev (a:x) = rev x ++ [a] </syntaxhighlight> which can be applied to a list of any data type, for which the explicit function type declaration would be: <syntaxhighlight lang="haskell"> rev :: [*] -> [*] </syntaxhighlight> Finally, it has mechanisms for creating and managing program [[module (programming)|module]]s whose internal functions are invisible to programs calling those modules.
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)