@fraczak/k 6.8.0 → 6.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -92,6 +92,7 @@ _fromBitList =
92
92
 
93
93
  --------------------------------------
94
94
  $ int = < bits '+', bits '-' >;
95
+ $ ints = <{int car, ints cdr} cons, {} nil>;
95
96
 
96
97
  remove_leading_zeros = <
97
98
  /0 remove_leading_zeros,
package/Examples/poly.k CHANGED
@@ -65,3 +65,49 @@ zip = <
65
65
  {.xs cdr xs, .ys cdr ys} zip cdr
66
66
  } cons
67
67
  >;
68
+
69
+ -- Gen list of intgers: n -> [0, 1, ..., n]
70
+ gen_list = {() n, nil list} gen_list_;
71
+ gen_list_ = <
72
+ {.n neg? if, .list then} .then,
73
+ {.n dec n, {.n car, .list cdr} cons list} gen_list_
74
+ >;
75
+
76
+ -- Identity filter on {x, y} when x is greater than y: ?{X x, X y}=Y -> ?Y
77
+ gt? =
78
+ <
79
+ {.x/- if1, .y/+ if2, () then} .then,
80
+ {{.x/- y, .y/- x} GT if, () then} .then,
81
+ {{.x/+ x, .y/+ y} GT if, () then} .then
82
+ >
83
+ ;
84
+
85
+ -- Split list into two halves by alternating elements: xs -> {first, second}
86
+ split = {() xs, nil first, nil second} _split;
87
+ _split = <
88
+ {.xs /nil if, {.first first, .second second} then} .then,
89
+ {.xs cdr /nil if, {{.xs car car, .first cdr} cons first, .second second} then} .then,
90
+ {.xs cdr cdr xs, {.xs car car, .first cdr} cons first, {.xs cdr car car, .second cdr} cons second} _split
91
+ >;
92
+
93
+ -- Merge two sorted lists: {xs, ys} -> list
94
+ merge = <
95
+ {.xs /nil if, .ys then} .then,
96
+ {.ys /nil if, .xs then} .then,
97
+ {
98
+ {.xs car x, .ys car y} gt? if,
99
+ {.ys car car, {.xs xs, .ys cdr ys} merge cdr} cons then
100
+ } .then,
101
+ {.xs car car, {.xs cdr xs, .ys ys} merge cdr} cons
102
+ >;
103
+
104
+ -- Sort a list in ascending order (merge sort): xs -> list
105
+ sort = list? <
106
+ /nil nil,
107
+ /cons <
108
+ {.cdr /nil if, .car singleton then} .then,
109
+ |cons split {.first sort xs, .second sort ys} merge
110
+ >
111
+ > list?;
112
+
113
+