Miranda (programming language)


Miranda is a lazy, 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 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. Miranda had a strong influence on the later Haskell programming language.

Overview

Miranda is a lazy, purely functional programming language. That is, it lacks side effects and imperative programming features. A Miranda program 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, 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 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 =

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 =

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

days = week_days ++
days = "Nil":days
days!0
⇒ "Nil"
days = days --
#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
odd_sum = sum

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

squares = ]

and a series where each term is a function of the previous one, e.g.:

powers_of_2 =

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

half =
reciprocal =

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 declarations. If a function's type is not explicitly declared, the interpreter infers it from the type of its parameters and how they are used within the function. In addition to the basic types, it includes an "anything" type where the type of a parameter does not matter, as in the list-reversing function:

rev =
rev = rev x ++

which can be applied to a list of any data type, for which the explicit function type declaration would be:

rev :: ->

Finally, it has mechanisms for creating and managing program modules whose internal functions are invisible to programs calling those modules.

Sample code

The following Miranda script determines the set of all subsets of a set of numbers

subsets =
subsets = x] ++ y | y <- ys] ++ ys
where ys = subsets xs

and this is a literate script for a function primes
which gives the list of all prime numbers

> || The infinite list of all prime numbers.
The list of potential prime numbers starts as all integers from 2 onwards;
as each prime is returned, all the following numbers that can exactly be
divided by it are filtered out of the list of candidates.
> primes = sieve
> sieve = p : sieve

Here, we have some more examples

max2 :: num -> num -> num
max2 a b = a, if a>b
= b, otherwise
max3 :: num -> num -> num -> num
max3 a b c = max2
multiply :: num -> num -> num
multiply 0 b = 0
multiply a b = a +
fak :: num -> num
fak 0 = 1
fak 1 = 1
fak n = n *
itemnumber::->num
itemnumber = 0
itemnumber = 1 + itemnumber x
weekday::= Mo|Tu|We|Th|Fr|Sa|Su
isWorkDay :: weekday -> bool
isWorkDay Sa = False
isWorkDay Su = False
isWorkDay anyday = True
tree * ::= E| N *
nodecount :: tree * -> num
nodecount E = 0
nodecount = nodecount l + 1 + nodecount r
emptycount :: tree * -> num
emptycount E = 1
emptycount = emptycount l + emptycount r
treeExample = N 3 ) 5 8 )
weekdayTree = N Tu ) Th Sa )
insert :: * -> stree * -> stree *
insert x E = N E x E
insert x = N l w x
insert x = N x w r
insert x = insert x l, if x = insert x r, otherwise
list2searchtree :: -> tree *
list2searchtree = E
list2searchtree = N E x E
list2searchtree = insert x
maxel :: tree * -> *
maxel E = error "empty"
maxel = w
maxel = maxel r
minel :: tree * -> *
minel E = error "empty"
minel = w
minel = minel l
preorder,inorder,postorder :: tree * ->
inorder E =
inorder N l w r = inorder l ++ ++ inorder r
preorder E =
preorder N l w r = ++ preorder l ++ preorder r
postorder E =
postorder N l w r = postorder l ++ postorder r ++
height :: tree * -> num
height E = 0
height = 1 + max2
amount :: num -> num
amount x = x,if x >= 0
amount x = x*, otherwise
and :: bool -> bool -> bool
and True True = True
and x y = False
isAvl :: tree * -> bool
isAvl E = True
isAvl = and , if amount - ) < 2
= False, otherwise



delete :: * -> tree * -> tree *
delete x E = E
delete x = E
delete x = N E
delete x = N r
delete x = N w