Lists
A list in square brackets behaves as one value: arithmetic on it applies element by element, and a function of a list returns a list.
"Lists"
N = [1, 2, 3, 4, 5, 6, 7, 8] @ hidden
folder "Building lists" { "Arithmetic maps over every element." squares = N ^ 2 @ hidden "join glues lists together, sort orders them." both = join(N, squares) @ hidden ordered = sort(both) @ hidden "repeat builds a list of one repeated value." flat = repeat(3, 5) @ hidden "L[3] indexes, length counts." third = ordered[3] @ hidden size = length(ordered) @ hidden "An index takes a range, and may leave an end off: from the fourth on, the first three." rest = ordered[4...] @ hidden first = ordered[...3] @ hidden}
folder "Lists as points" { "Two lists paired coordinate-wise become a list of points." (N, N ^ 2) @ color: RED, pointSize: 12 "One list against a function of it." (N, sqrt(N)) @ color: BLUE, pointSize: 12 "lines connects them into a path, and points: false leaves only that." (N, 0.5N) @ color: GREEN, lines, points: false, lineWidth: 3}
folder "A list of graphs" { "A list where a constant would go draws one curve per element." K = [-3, -2, -1, 0, 1, 2, 3] @ hidden y = x + K @ color: PURPLE, lineOpacity: 0.6 "The same trick on a family of circles." R = [1, 2, 3, 4] @ hidden x ^ 2 + y ^ 2 = R ^ 2 @ color: ORANGE, lineOpacity: 0.7}
folder "Lists built from a rule" { "[1...10] is the range; `for` runs an expression over it." S = [i ^ 2 for i = [1...10]] @ hidden "The comprehension is a list like any other." (S, sqrt(S)) @ color: RED, pointSize: 12 "Two variables after `for` pair off every combination." G = [(a, b) for a = [-4...4], b = [1, 2]] @ color: BLUE, pointSize: 8, lines: false "random() draws a number in [0, 1); random(n) draws a list of n." seed = random() @ hidden noise = random(length(N)) @ hidden (N, noise) @ color: GREEN, pointSize: 12, lines: false}Open in playground →