|
CS 636 / 338 The Structure of Programming Languages Miranda: A Pure Functional Language |
| gcd a b | = gcd (a-b) b, | a > b | |
| = gcd a (b-a), | a > b | ||
| = a, | a = b |
| quadroot a b c | = error "complex roots", | delta < 0 | |
| = [term1], | delta = 0 | ||
| = [term1+term2, term1-term2], | delta > 0 | ||
| where | |||
| delta = b*b - 4*a*c | |||
| term1 = -b/(2*a) | |||
| term2 = radix/(2*a) | |||
| radix = sqrt delta |
| (a) | [ n | n ← [1..5] ] | Five consecutive integers: [1, 2, 3, 4, 5] |
| (b) | [ n | n ← [1..] ] | An infinite list--all positive integers |
| (c) | [ n | n ← [2, 4..10] ] | An arithmetic series: [2, 4, 6, 8, 10] |
| (d) | [ n+2 | n ← [1..5] ] | [3, 4, 5, 6, 7] |
| (e) | [ n*2 | n ← [2, 4..10] ] | [4, 8, 12, 16, 20] |
| (f) | [ n*n | n ← [1, 2 .. ] ] | All perfect squares: 1, 4, 9, etc. |
| (g) | [ x+y | x ← [1..3]; y ← [3, 7] ] | All combinations of an x plus a y: [4, 5, 6, 8, 9, 10 ] |
| (h) | [ x*y | x ← [1..3]; y ← [1..3]; x>=y ] | All combinations of an x and a y are generated, but the value pairs are compared before being used to generate a list element. A pair that fails the test is thrown out, a pair that passes is used to compute x*y and the result is put into the answer list: [1, 2, 4, 3, 6, 9] |
| ZF expr |
::= '[' body '|' |
|
| qual-list | ::= generator { generator | filter } | |
| generator | ::= variable ' ←' list-expression | |
| filter | ::= Boolean-expression | |
| body | ::= list-expression | |
| list expression | ::= literal-list | infinite-list-denotation | arithmetic-series-denotation |
| pow10 0 | = 1 | |
| pow10 (n+1) | = 10 * powlist ! n | |
| powlist | = [ pow10(x) | x ← [0..] ] |
| sort [] = [] | ||
| sort (pivot:rest) = | sort [ y | y ← rest; b <= pivot ] | |
| ++ [pivot] ++ | ||
| sort [ z | z ← rest; b > pivot ] |
| Last updated: 5/8/03 |