Miranda (programming language): Difference between revisions

Content deleted Content added
m tree,putting->tree, putting - Fix a typo in one click
Tags: Mobile edit Mobile web edit Advanced mobile edit
m Task 70: Update syntaxhighlight tags - remove use of deprecated <source> tags
Line 42:
 
[[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:
<sourcesyntaxhighlight lang="haskell">
this_employee = ("Folland, Mary", 10560, False, 35)
</syntaxhighlight>
</source>
 
The ''[[list (computing)|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:
 
<sourcesyntaxhighlight lang="haskell">
week_days = ["Mon","Tue","Wed","Thur","Fri"]
</syntaxhighlight>
</source>
List concatenation is <code>++</code>, subtraction is <code>--</code>, construction is <code>:</code>, sizing is <code>#</code> and indexing is <code>!</code>, so:
 
<sourcesyntaxhighlight lang="clean">
days = week_days ++ ["Sat","Sun"]
days = "Nil":days
Line 61:
#days
⇒ 7
</syntaxhighlight>
</source>
 
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:
 
<sourcesyntaxhighlight lang="haskell">
fac n = product [1..n]
odd_sum = sum [1,3..100]
</syntaxhighlight>
</source>
 
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.:
 
<sourcesyntaxhighlight lang="haskell">
squares = [ n * n | n <- [1..] ]
</syntaxhighlight>
</source>
 
(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.:
 
<sourcesyntaxhighlight lang="haskell">
powers_of_2 = [ n | n <- 1, 2*n .. ]
</syntaxhighlight>
</source>
 
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>
Line 88:
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)|parameter]]s 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 [[currying|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:
 
<sourcesyntaxhighlight lang="haskell">
add a b = a + b
increment = add 1
</syntaxhighlight>
</source>
 
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>.
Line 98:
Thus:
 
<sourcesyntaxhighlight lang="haskell">
increment = (+) 1
</syntaxhighlight>
</source>
 
is the briefest way to create a function that adds one to its argument. Similarly, in
 
<sourcesyntaxhighlight lang="haskell">
half = (/ 2)
reciprocal = (1 /)
</syntaxhighlight>
</source>
 
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.
Line 113:
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:
 
<sourcesyntaxhighlight lang="haskell">
rev [] = []
rev (a:x) = rev x ++ [a]
</syntaxhighlight>
</source>
 
which can be applied to a list of any data type, for which the explicit function type declaration would be:
 
<sourcesyntaxhighlight lang="haskell">
rev :: [*] -> [*]
</syntaxhighlight>
</source>
 
Finally, it has mechanisms for creating and managing program [[module (programming)|module]]s whose internal functions are invisible to programs calling those modules.
Line 130:
The following Miranda script determines the set of all subsets of a set of numbers
 
<sourcesyntaxhighlight lang="haskell">
subsets [] = [[]]
subsets (x:xs) = [[x] ++ y | y <- ys] ++ ys
where ys = subsets xs
</syntaxhighlight>
</source>
 
and this is a literate script for a function <code>primes</code>
which gives the list of all prime numbers
 
<sourcesyntaxhighlight lang="console">
> || The infinite list of all prime numbers.
 
Line 148:
> primes = sieve [2..]
> sieve (p:x) = p : sieve [n | n <- x; n mod p ~= 0]
</syntaxhighlight>
</source>
 
Here, we have some more examples