summaryrefslogtreecommitdiff
path: root/miralib/manual/13
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2022-03-04 12:32:20 -0500
committerJakob Kaivo <jkk@ung.org>2022-03-04 12:32:20 -0500
commit55f277e77428d7423ae906a8e1f1324d35b07a7d (patch)
tree5c1c04703dff89c46b349025d2d3ec88ea9b3819 /miralib/manual/13
import Miranda 2.066 from upstream
Diffstat (limited to 'miralib/manual/13')
-rw-r--r--miralib/manual/13/122
-rw-r--r--miralib/manual/13/268
-rw-r--r--miralib/manual/13/333
-rw-r--r--miralib/manual/13/contents6
4 files changed, 129 insertions, 0 deletions
diff --git a/miralib/manual/13/1 b/miralib/manual/13/1
new file mode 100644
index 0000000..c5e9198
--- /dev/null
+++ b/miralib/manual/13/1
@@ -0,0 +1,22 @@
+_D_o_t_d_o_t_ _n_o_t_a_t_i_o_n
+
+The following abbreviations are provided for denoting lists, of type
+[num], whose members form a finite or infinite arithmetic series. Let
+`a', `b', `c' stand for arbitrary numeric expressions.
+
+ [a..b] list of numbers from a to b inclusive, interval = 1
+ [a..] infinite list starting at a and increasing by 1
+ [a,b..c] arithmetic series, first member a, second member b,
+ last member not greater than c (if b-a non-negative)
+ or not less than c (if b-a negative).
+ [a,b..] infinite series starting at a, interval = (b-a)
+
+So the notation [1..10] has as value the list [1,2,3,4,5,6,7,8,9,10].
+Here are some more examples
+
+ nats = [0..]
+ evens = [0,2..]
+ odds_less_than_100 = [1,3..99]
+ neg_odds = [-1,-3..]
+ tenths = [1.0,1.1 .. 2.0]
+
diff --git a/miralib/manual/13/2 b/miralib/manual/13/2
new file mode 100644
index 0000000..2e86d88
--- /dev/null
+++ b/miralib/manual/13/2
@@ -0,0 +1,68 @@
+_L_i_s_t_ _c_o_m_p_r_e_h_e_n_s_i_o_n_s
+
+ [exp | qualifiers]
+
+List of all `exp' such that `qualifiers'. If there are two or more
+qualifiers they are separated by semicolons. Each qualifier is either a
+generator, of which the allowed forms are
+
+ pattern-list <- exp (first form)
+
+ pattern <- exp, exp .. (second form)
+
+or else a filter, which is a boolean expression restricting the range of
+the variables introduced by preceding generators. The variables
+introduced on the left of each `<-' are local to the list comprehension.
+
+Some examples
+
+ sqs = [ n*n | n<-[1..] ]
+
+ factors n = [ r | r<-[1..n div 2]; n mod r = 0 ]
+
+ knights_moves [i,j] = [ [i+a,j+b] | a,b<-[-2..2]; a^2+b^2=5 ]
+
+Notice that a list of variables on the lhs of a `<-' is shorthand for
+multiple generators, e.g. `i,j<-thing' expands to `i<-thing; j<-thing'.
+
+The variables introduced by the generators come into scope from left to
+right, so later generators can make use of variables introduced by
+earlier ones. An example of this is shown by the following definition
+of a function for generating all the permutations of a given list.
+
+ perms [] = [[]]
+ perms x = [ a:p | a<-x; p<-perms(x--[a]) ]
+
+The second form of generator allows the construction of lists from
+arbitrary recurrence relations, thus
+ x <- a, f x ..
+causes x to assume in turn the values `a', `f a', `f(f a)', etc.
+
+An example of its use is in the following definition of the fibonacci
+series
+
+ fibs = [ a | (a,b) <- (1,1), (b,a+b) .. ]
+
+Another example is given by the following expression which lists the
+powers of two
+
+ [ n | n <- 1, 2*n .. ]
+
+The order of enumeration of a list comprehension with multiple
+generators is like that of nested for-loops, with the rightmost
+generator as the innermost loop. For example the value of the
+comprehension [ f x y | x<-[1..4]; y<-[1..4] ] is
+
+ [ f 1 1, f 1 2, f 1 3, f 1 4, f 2 1, f 2 2, f 2 3, f 2 4,
+ f 3 1, f 3 2, f 3 3, f 3 4, f 4 1, f 4 2, f 4 3, f 4 4 ]
+
+As a consequence of this order of enumeration of multiple generators, if
+any generator other than the first (leftmost) is infinite, some
+combinations of values will never be reached in the enumeration. To
+overcome this a second, _d_i_a_g_o_n_a_l_i_s_i_n_g, form of list comprehension is
+provided (see separate manual section).
+
+Note that list comprehensions do NOT remove duplicates from the result
+list. To remove duplicates from a list, apply the standard function
+`mkset'.
+
diff --git a/miralib/manual/13/3 b/miralib/manual/13/3
new file mode 100644
index 0000000..9cfbf11
--- /dev/null
+++ b/miralib/manual/13/3
@@ -0,0 +1,33 @@
+_D_i_a_g_o_n_a_l_i_s_i_n_g_ _l_i_s_t_ _c_o_m_p_r_e_h_e_n_s_i_o_n_s
+
+ [ exp // qualifiers ]
+
+Syntax and scope rules exactly as for standard list comprehensions, the
+only difference being the use of `//' in place of the vertical bar. The
+order of enumeration of the generators is such that it is guaranteed
+that every possible combination of values will be reached eventually.
+The diagonalisation algorithm used is "fair" in the sense that it gives
+equal priority to all of the generators.
+
+For example the value of [f x y//x<-[1..4]; y<-[1..4]] is
+
+ [ f 1 1, f 1 2, f 2 1, f 1 3, f 2 2, f 3 1, f 1 4, f 2 3,
+ f 3 2, f 4 1, f 2 4, f 3 3, f 4 2, f 3 4, f 4 3, f 4 4 ]
+
+The algorithm used used is "Cantorian diagonalisation" - imagine the
+possible combinations of values from the two generators laid out in a
+(perhaps infinite) rectangular array, and traverse each diagonal in turn
+starting from the origin. The appropriate higher-dimensional analogue
+of this algorithm is used for the case of a list comprehension with
+three or more generators.
+
+As an example of an enumeration that could not be defined at all using a
+standard list comprehension, because of the presence of several infinite
+generators, here is a definition of the list of all pythagorean
+triangles (right-angled triangles with integer sides)
+
+ pyths = [(a,b,c)//a,b,c<-[1..];a^2+b^2=c^2]
+
+In the case that there is only one generator, the use of `//' instead of
+`|' makes no difference to the meaning of the list comprehension.
+
diff --git a/miralib/manual/13/contents b/miralib/manual/13/contents
new file mode 100644
index 0000000..543af82
--- /dev/null
+++ b/miralib/manual/13/contents
@@ -0,0 +1,6 @@
+_I_t_e_r_a_t_i_v_e_ _e_x_p_r_e_s_s_i_o_n_s
+
+ 1. Dotdot expression
+ 2. List comprehensions
+ 3. Diagonalising list comprehensions
+