raz / conform / run / ConstSlice.raz

56 lines · 1.3 KiB · raw · history · last change 264716cb

1(* []const T (report section 5.3): its elements are read and it is
2 sliced; a []T, an array, a string or [] goes where one is wanted;
3 another module's exported array is read as one (section 3); BYTES
4 of one is a []const U8; and it compares and is matched as a []T. *)
5module ConstSlice;
6import Digits, Out;
7
8type Shape = variant dot; box(w: INT); end;
9
10(* How many times c is in s, which it only reads *)
11proc count(s: []const U8; c: U8): INT;
12var i, n: INT;
13begin
14 for i := 0 to LEN(s) - 1 do
15 if s[i] = c then INC(n); end;
16 end;
17 return n;
18end;
19
20(* The first two elements of s, as s is: a []const *)
21proc head(s: []const U8): []const U8;
22begin
23 return s[0..2];
24end;
25
26proc main*;
27var a: [5]U8; w: []const U8; r, b: []const U8; shapes: [2]Shape; cs: []const Shape;
28begin
29 a := "hello";
30 w := a;
31 Out.int(count(a, 'l'));
32 Out.int(count(w, 'h'));
33 Out.int(count("all", 'l'));
34 Out.int(count([], 'l'));
35 r := head(w);
36 if (r = "he") and (r = w[0..2]) then Out.char('='); end;
37 match r of
38 | "he": Out.char('m');
39 | else:
40 end;
41 r := Digits.digits;
42 Out.int(INT(r[9]) - '0');
43 r := Digits.digits[3..5];
44 Out.int(LEN(r));
45 b := BYTES(r);
46 Out.int(LEN(b));
47 shapes[1] := box(7);
48 cs := shapes;
49 match cs[1] of
50 | box(width): Out.int(width);
51 | dot:
52 end;
53 Out.char(10);
54end;
55
56end ConstSlice.