Miranda (programming language)

related topics
{math, number, function}
{system, computer, user}
{day, year, event}

Miranda is a non-strict purely functional programming language designed by David Turner as a successor to his earlier programming languages SASL and KRC, using some concepts from ML and Hope. It was produced by Research Software Ltd. of England (which holds a trademark on the name Miranda) and was the first purely functional language to be commercially supported.

Miranda was first released in 1985, as a fast interpreter in C for Unix-flavour operating systems, with subsequent releases in 1987 and 1989. The later Haskell programming language is similar in many ways to Miranda.

Overview

Miranda is a lazy, purely functional programming language. That is, it lacks side effects and imperative programming features. A Miranda program (called a script) is a set of equations that define various mathematical functions and algebraic data types. The word 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), there is rarely a need for bracketing statements and no statement terminators are required. This feature, inspired by ISWIM is also used in occam and Haskell and was later popularized by Python.

Commentary is introduced into regular scripts by the characters || and continue to the end of the same line. An alternative commenting convention affects an entire source code file, known as a "literate script", in which every line is considered a comment unless it starts with a > sign.

Miranda's basic data types are char, num and bool. A character string is simply a list of char, while num is silently converted between two underlying forms: arbitrary-precision integers (a.k.a. bignums) by default, and regular floating point values as required.

Tuples are sequences of elements of potentially mixed types, analogous to records in Pascal-like languages, and are written delimited with parentheses:

 this_employee = ("Folland, Mary", 10560, False, 35)

The 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:

 week_days = ["Mon","Tue","Wed","Thur","Fri"]

List concatenation is ++, subtraction is --, construction is :, sizing is # and indexing is !, so:

 days = week_days ++ ["Sat","Sun"]
 days = "Nil":days
 days!0
→ "Nil"
 days = days -- ["Nil"]
 #days
→ 7

There are several list-building shortcuts: .. is used for lists whose elements form an arithmetic series, with the possibility for specifying an increment other than 1:

 fac n   = product [1..n]
 odd_sum = sum [1,3..100]

More general and powerful list-building facilities are provided by "list comprehensions" (previously known as "ZF expressions"), which come in two main forms: an expression applied to a series of terms, e.g.:

 squares = [ n * n | n <- [1..] ]

(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.:

 powers_of_2 = [ n | n <- 1, 2*n .. ]

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: [1..]

The notation for function application is simply juxtaposition, as in sin x.

In Miranda, as in most other purely functional languages, functions are first-class citizens, which is to say that they can be passed as parameters to other functions, returned as results, or included as elements of data structures. What is more, a function requiring two or more parameters may be "partially parameterised", or curried, by supplying less than the full number of parameters. This gives another function which, given the remaining parameters, will return a result. For example:

 add a b = a + b
 increment = add 1

is a roundabout way of creating a function "increment" which adds one to its argument. In reality, add 4 7 takes the two-parameter function add, applies it to 4 obtaining a single-parameter function that adds four to its argument, then applies that to 7.

Any function taking two parameters can be turned into an infix operator (for example, given the definition of the add function above, the term $add is in every way equivalent to the + operator) and every infix operator taking two parameters can be turned into a corresponding function. Thus:

 increment = (+) 1

is the briefest way to create a function that adds one to its argument. Similarly, in

Full article ▸

related documents
Elementary algebra
Ideal (ring theory)
Abel–Ruffini theorem
Unlambda
Gödel's completeness theorem
Natural logarithm
Division (mathematics)
Locally compact space
Trace (linear algebra)
Cauchy–Schwarz inequality
Power series
String (computer science)
Chinese remainder theorem
Transposition cipher
Isomorphism
Holomorphic function
Normed vector space
Rice's theorem
J (programming language)
Logical connective
Brute-force search
Ordinary differential equation
Merge sort
Tychonoff's theorem
Objective Caml
Scope (programming)
Field extension
Homological algebra
Key size
Preadditive category