# Miranda语言

编程范型 惰性求值, 纯函数式, 声明式 Research Software Ltd 1985年，​38年前 强类型, 静态 二条款BSD许可证 Miranda homepage Miranda KRC, ML, SASL, Hope Clean, Haskell,

Miranda，是一種惰性求值纯函数式編程語言，由英國學者所設計。採用來自MLHope语言的概念，他用此來作為他先前所設計的SASLKRC语言的後繼者[1]。這個程式語言由英國的研究軟體公司出品，這間公司擁有這個程式語言的商標權。

## 概述

Miranda是惰性纯函数式编程语言。就是说，它缺少副作用指令式编程特征。一个Miranda程序（叫做脚本）定义数学函数代数数据类型的一组等式集合的概念在这里是重要的，即等式的次序一般而言是无关紧要的，并且不需要在使用之前定义一个实体。

### 数据类型

Miranda的基本数据类型`char``num``bool`。字符串简单的就是`char`的列表，而`num`在两种底层形式之间静默转换：缺省的任意精度整数（又名bignum），和需要时的正规浮点值。

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

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

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

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

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

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

### 函数

```add a b = a + b
```

```increment = (+) 1
```

```half = (/ 2)
reciprocal = (1 /)
```

### 类型推论和多态

```rev [] = []
rev (a:x) = rev x ++ [a]
```

```rev :: [*] -> [*]
```

## 样例代码

```subsets []     = [[]]
subsets (x:xs) = [[x] ++ y | y <- ys] ++ ys
where ys = subsets xs
```

```> || 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 [2..]
> sieve (p:x) = p : sieve [n | n <- x; n mod p ~= 0]
```

## 引用

1. ^ Turner, D. A. Some History of Functional Programming Languages (PDF). [2020-04-25]. （原始内容存档 (PDF)于2020-04-15）.
2. ^ Hudak, Paul; Hughes, John. A History of Haskell: being lazy with class. 2007 [2021-02-27]. （原始内容存档于2016-12-26）.