Miranda (编程语言) - Wikiwand

# Miranda (编程语言)

## 维基百科，自由的百科全书

编程范型 惰性求值, 纯函数式, 声明式 大卫·特纳 Research Software Ltd 1985年，​36年前 2.066 （2020年1月31日，​19个月前 ） 强类型, 静态 二条款BSD许可证 Miranda homepage Miranda KRC, ML, SASL, Hope Clean, Haskell, Orwell

Miranda，是一种惰性求值纯函数式编程语言，由英国学者大卫·特纳所设计。采用来自MLHope语言的概念，他用此来作为他先前所设计的SASLKRC语言的后继者[1]。这个编程语言由英国的研究软件公司出品，这间公司拥有这个编程语言的商标权。

## 概述

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

### 数据类型

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.