-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchurch_builtins.js
2423 lines (2175 loc) · 66.2 KB
/
church_builtins.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global global, require, module, exports */
var seedrandom = require('seedrandom');
// Contains the built-in Church functions written in Javascript.
// TODO: document annotations format
// style guide: in parameters to builtin functions, name them like 'lst' rather than 'list'
// to call the list making function, i.e., the (list ...) function in church, use javascript
// function called List (or, to create a pair in javascript, use Pair)
// represents pairs as arrays
// represents lists as arrays with null as the last element
// TODO: move the asserts stuff into its own library
// it depends on two utility functions, though:
// _rest and listToArray
// maybe basic types stuff should live in a module
// separate from builtins and separate from asserts
var typeCheckers = {
'integer': function(x) {
return typeof x == 'number' && Math.floor(x) == x;
},
nat: function(x) {
return typeof x == 'number' && Math.floor(x) == x && x >= 0;
},
'positive real': function(x) {
return typeof x == 'number' && x > 0;
},
real: function(x) {
return typeof x == 'number';
},
function: function(x) {
return typeof x == 'function';
},
pair: function(x) {
return Array.isArray(x) && x.length >= 2;
},
list: function(x) {
return Array.isArray(x) && x[x.length - 1] == null;
},
'boolean': function(x) {
return typeof x == 'boolean';
},
'string': function(x) {
return typeof x == 'string';
}
};
// handle simple parameterized types like
// List real, Pair real
// TODO: test this
function parseTypeString(s) {
if (/list|pair/.test(s)) {
var baseType = /list/.test(s) ? 'list' : 'pair';
var uStart = s.indexOf("<");
var uEnd = s.lastIndexOf(">");
var baseChecker = typeCheckers[baseType];
if (uStart == -1 || uEnd == -1) {
return baseChecker;
}
var u = s.slice(uStart + 1, uEnd);
var uChecker = parseTypeString(u);
if (baseType == 'pair') {
return function(x) {
if (!baseChecker(x)) {
return false;
}
return uChecker(x[0]) && uChecker(_rest(x));
};
}
// otherwise, return checker for list<...>
return function(x) {
if (!baseChecker(x)) {
return false;
}
var x_array = listToArray(x);
for(var i = 0, ii = x_array.length; i < ii; i++) {
if (!uChecker(x_array[i])) {
return false;
};
}
return true;
};
} else {
return typeCheckers[s];
}
}
// TODO: underscore is too heavy weight
// replace with mustache. or maybe something even dumber
var _ = require('underscore');
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
var util = require('./util.js');
var fs = require('fs');
var typeUtils = require('./type-utils.js');
var listToArray = typeUtils.listToArray;
var arrayToList = typeUtils.arrayToList;
// determine whether we're running inside a browser
var inBrowser = false;
if (typeof document !== 'undefined') {
inBrowser = true;
}
// var seed = require('seed-random');
// var set_seed = function(str) {
// seed(str, {global: true});//over-ride global Math.random
// };
module.exports.__annotations__ = {};
// add a Church builtin
// note: this transforms the alias property into
// an array containg zero or more aliases
var addBuiltin = function(dict) {
var fWrapped = wrapAsserts(dict);
// if alias is just a single string, embed it in an array
if (typeof dict.alias == "string") {
dict.alias = [dict.alias];
}
if (!dict.alias) {
dict.alias = [];
}
// add the automated alias
var autoAlias = dict.name
.replace(/wrapped_(.+)/, function(m, p1) { return p1 })
.replace(/is_(.+)/, function(m, p1) { return p1 + "?"})
.replace('_to_', '->')
.replace(/_/g, '-');
if (dict.name !== autoAlias) {
dict.alias.push(autoAlias);
}
module.exports[dict.name] = fWrapped;
module.exports.__annotations__[dict.name] = dict;
return fWrapped;
};
var $b = addBuiltin;
var the_empty_list = [];
function sizeof(obj) { return Object.keys(obj).length; }
// needs to live in global scope
// but users shouldn't need to directly call this function
// so don't add it to annotations
var args_to_array = module.exports.args_to_list = function(args) {
return Array.prototype.slice.call(args, 0 );
};
// needs to live in global scope
// but users shouldn't need to directly call this function
// so don't add it to annotations
var args_to_list = module.exports.args_to_list = function (args) {
return arrayToList(args_to_array(args));
};
function atLeastOne (args) {
if (args.length < 1) {throw new Error('Needs at least one argument');};
}
var js_debug = $b({
name: 'js_debug',
desc: 'Trigger the javascript debugger',
fn: function() {
debugger;
}
});
var plus = $b({
name: 'plus',
alias: '+',
desc: "Add numbers",
params: [{name: '[x ...]', type: 'real', desc: 'Numbers to add'}],
fn: function () {
atLeastOne(arguments);
var sum = 0;
for (var i = 0, ii = arguments.length; i < ii; i++) {
sum = sum + arguments[i];
}
return sum;
}
});
var minus = $b({
name: 'minus',
alias: '-',
desc: "Subtract numbers",
params: [{name: '[x ...]', type: 'real', desc: 'Numbers to subtract'}],
fn: function() {
atLeastOne(arguments);
var numArgs = arguments.length;
if (numArgs == 1) {
return -arguments[0];
} else {
var r = arguments[0];
for (var i = 1; i < numArgs; i++) {
r -= arguments[i];
}
return r;
}
}
});
var mult = $b({
name: 'mult',
alias: '*',
desc: "Multiply numbers",
params: [{name: '[x ...]', type: 'real', desc: 'Numbers to multiply'}],
fn: function() {
atLeastOne(arguments);
var numArgs = arguments.length;
var prod = 1;
for (var i = 0; i < numArgs; i++) {
prod = prod * arguments[i];
}
return prod;
}
});
var div = $b({
name: 'div',
alias: '/',
desc: "Divide numbers. Returns x / (y1 * y2 * ... )",
params: [{name: '[x]', type: 'real', desc: 'Numerator'},
{name: '[y ...]', type: 'real', desc: 'Denominator values'}
],
fn: function() {
atLeastOne(arguments);
var numerator = arguments[0];
var numArgs = arguments.length;
if (numArgs == 1) {
return 1 / arguments[0];
} else {
var denominator = 1;
for (var i = 1; i < numArgs; i++) {
denominator *= arguments[i];
}
return numerator / denominator;
}
}
});
var mod = $b({
name: 'mod',
alias: 'modulo',
desc: "Modulo. Returns x mod y",
params: [{name: 'x', type: 'real'},
{name: 'y', type: 'real'}],
fn: function(x,y) {
return x % y;
}
});
var round = $b({
name: 'round',
desc: 'Round a number',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.round(x);
}
});
var floor = $b({
name: 'floor',
desc: 'Floor of a number',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.floor(x);
}
});
var ceil = $b({
name: 'ceil',
alias: 'ceiling',
desc: 'Ceiling of a number',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.ceil(x);
}
});
var abs = $b({
name: 'abs',
desc: 'Absolute value',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.abs(x);
}
});
var log = $b({
name: 'log',
desc: 'Natural logarithm',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.log(x);
}
});
var exp = $b({
name: 'exp',
desc: 'Exponential',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.exp(x);
}
});
var expt = $b({
name: 'expt',
alias: ['pow','expt'],
desc: 'Compute x raised to the power y',
params: [{name: 'x', type: 'real'},
{name: 'y', type: 'real'}
],
fn: function(x, y) {
return Math.pow(x, y);
}
});
var sqrt = $b({
name: 'sqrt',
desc: 'Square root',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.sqrt(x);
}
});
var sin = $b({
name: 'sin',
desc: 'Sine',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.sin(x);
}
});
var cos = $b({
name: 'cos',
desc: 'Cosine',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.cos(x);
}
});
var tan = $b({
name: 'tan',
desc: 'Tangent',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.tan(x);
}
});
var asin = $b({
name: 'asin',
desc: 'Arcsine',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.asin(x);
}
});
var acos = $b({
name: 'acos',
desc: 'Arccosine',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.acos(x);
}
});
var atan = $b({
name: 'atan',
desc: 'Arctangent',
params: [{name: 'x', type: 'real'}],
fn: function(x) {
return Math.atan(x);
}
});
var atan2 = $b({
name: 'atan2',
desc: 'Arctangent (quotient version)',
params: [{name: 'y', type: 'real'},
{name: 'x', type: 'real'}],
fn: function(y, x) {
return Math.atan2(y, x);
}
});
var sum = $b({
name: 'sum',
desc: 'Sum a list of numbers',
params: [{name: 'lst', type: 'list<real>', desc: 'List of numbers to sum'}],
fn: function(lst) {
return _.foldl(listToArray(lst), function(a,b){return a+b;}, 0);
}
});
var prod = $b({
name: 'prod',
desc: 'Multiply a list of numbers',
params: [{name: 'lst', type: 'list<real>', desc: 'List of numbers to multiply'}],
fn: function(lst) {
return _.foldl(listToArray(lst), function(a,b){return a*b;}, 1);
}
});
// check whether y \in (x - tol, x + tol)
var soft_equal = $b({
name: 'soft_equal',
alias: ['soft='],
desc: 'Check whether y is in the interval [x - tol, x + tol]',
params: [{name: 'y', type: 'real'},
{name: 'x', type: 'real'},
{name: 'tol', type: 'real'}
],
fn: function(y, x, tol) {
// FIXME: assert upper > lower
return (y > x - tol && y < x + tol);
}
});
var and = $b({
name: 'and',
desc: 'Logical conjunction',
params: [{name: '[b ...]', type: 'boolean', desc: 'Boolean values'}],
fn: function() {
return _.every(arguments)
}
});
var or = $b({
name: 'or',
desc: 'Logical disjunction',
params: [{name: '[b ...]', type: 'boolean', desc: 'Boolean values'}],
fn: function() {
return _.some(arguments)
}
});
var not = $b({
name: 'not',
desc: 'Logical negation',
params: [{name: 'b', type: 'boolean', desc: 'Boolean value'}],
fn: function(b) {
return !b;
}
});
var all = $b({
name: 'all',
desc: 'Test whether all of the values in a list are true',
params: [{name: 'lst', type: 'list<boolean>', desc: 'List of boolean values'}],
fn: function(lst) {
return and.apply(null, listToArray(lst));
}
});
var none = $b({
name: 'none',
desc: 'Test whether none of the values in a list are true',
params: [{name: 'lst', type: 'list<boolean>', desc: 'List of boolean values'}],
fn: function(lst) {
return !or.apply(null, listToArray(lst));
}
});
var some = $b({
name: 'some',
alias: 'any',
desc: 'Test whether some of the values in a list are true',
params: [{name: 'lst', type: 'list<boolean>', desc: 'List of boolean values'}],
fn: function(lst) {
return or.apply(null, listToArray(lst));
}
});
// there is a case to be made that this needs to be pairwise not first vs rest
// maybe make alternate versions that are pairwise
var greater = $b({
name: 'greater',
alias: '>',
desc: 'Test whether x is greater than all y\'s',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
var x = arguments[0];
for (var i = 1; i < numArgs ; i++) {
if (!(x > arguments[i])) {
return false;
}
}
return true;
}
});
var less = $b({
name: 'less',
alias: '<',
desc: 'Test whether x is less than all y\'s',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
var x = arguments[0];
for (var i = 1; i < numArgs ; i++) {
if (!(x < arguments[i])) {
return false;
}
}
return true;
}
});
var geq = $b({
name: 'geq',
alias: '>=',
desc: 'Test whether x is greater than or equal to all y\'s',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
var x = arguments[0];
for (var i = 1; i < numArgs ; i++) {
if (x < arguments[i]) {
return false;
}
}
return true;
}
});
var leq = $b({
name: 'leq',
alias: '<=',
desc: 'Test whether x is less than or equal to all y\'s',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
var x = arguments[0];
for (var i = 1; i < numArgs ; i++) {
if (x > arguments[i]) {
return false;
}
}
return true;
}
});
var pw_greater = $b({
name: 'pw_greater',
alias: '.>.',
desc: 'Test whether greater applies transitively',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
for (var i = 0, j = 1; j < numArgs ; i++, j++) {
if (!(arguments[i] > arguments[j])) {
return false;
}
}
return true;
}
});
var pw_less = $b({
name: 'pw_less',
alias: '.<.',
desc: 'Test whether less than applies transitively',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
for (var i = 0, j = 1; j < numArgs ; i++, j++) {
if (!(arguments[i] < arguments[j])) {
return false;
}
}
return true;
}
});
var pw_geq = $b({
name: 'pw_geq',
alias: '.>=.',
desc: 'Test whether greater than or equal to applies transitively',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
for (var i = 0, j = 1; j < numArgs ; i++, j++) {
if (!(arguments[i] >= arguments[j])) {
return false;
}
}
return true;
}
});
var pw_leq = $b({
name: 'pw_leq',
alias: '.<=.',
desc: 'Test whether less than or equal to applies transitively',
params: [{name: 'x', type: 'real'},
{name: '[y ...]', type: 'real'}
],
fn: function() {
var numArgs = arguments.length;
for (var i = 0, j = 1; j < numArgs ; i++, j++) {
if (!(arguments[i] <= arguments[j])) {
return false;
}
}
return true;
}
});
var eq = $b({
name: 'eq',
alias: '=',
canonical: '=',
desc: 'Test whether all (numeric) arguments are equal',
params: [{name: '[x ...]', type: 'real'}],
fn: function() {
var numArgs = arguments.length;
var x = arguments[0];
for (var i = 1; i < numArgs ; i++) {
if (x != arguments[i]) {
return false;
}
}
return true;
}
});
var is_null = $b({
name: 'is_null',
desc: 'Test whether x is null',
params: [{name: 'x'}],
fn: function(x) {
return Array.isArray(x) && x.length == 1 && x[0] == null;
}
});
// use uppercase to indicate that it's a constructor
var List = $b({
name: 'list',
desc: 'List constructor',
params: [{name: '[...]'}],
fn: function() {
var args = args_to_array(arguments);
return arrayToList(args, true);
}
});
var is_list = $b({
name: 'is_list',
desc: 'Test whether x is a list',
params: [{name: 'x'}],
fn: function(x) {
return Array.isArray(x) && x[x.length-1] == null;
}
});
var Pair = $b({
name: 'pair',
alias: 'cons',
desc: 'Pair constructor',
params: [{name: 'head'},
{name: 'tail'}
],
fn: function(head, tail) {
return [head].concat(tail);
}
});
var is_pair = $b({
name: 'is_pair',
desc: 'Test whether x is a pair',
params: [{name: 'x'}],
fn: function(x) {
return Array.isArray(x) && x.length >= 2;
}
});
var first = $b({
name: 'first',
alias: 'car',
desc: 'Get the first item of a list (or pair)',
params: [{name: 'lst', type: 'pair'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 1) {
throw new Error('Tried to get the first element of an empty list');
}
return arr[0];
}
});
var second = $b({
name: 'second',
desc: 'Get the second item of a list',
params: [{name: 'lst', type: 'list'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 2) {
throw new Error('Tried to get the 2nd element of a list with only ' + arr.length + ' item');
}
return arr[1];
}
});
var third = $b({
name: 'third',
desc: 'Get the third item of a list',
params: [{name: 'lst', type: 'list'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 3) {
throw new Error('Tried to get the 3rd element of list with only ' + arr.length + ' elements');
}
return arr[2];
}
});
var fourth = $b({
name: 'fourth',
desc: 'Get the fourth item of a list',
params: [{name: 'lst', type: 'list'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 4) {
throw new Error('Tried to get the 4th element of list with only ' + arr.length + ' elements');
}
return arr[3];
}
});
var fifth = $b({
name: 'fifth',
desc: 'Get the fifth item of a list',
params: [{name: 'lst', type: 'list'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 5) {
throw new Error('Tried to get the 5th element of list with only ' + arr.length + ' elements');
}
return arr[4];
}
});
var sixth = $b({
name: 'sixth',
desc: 'Get the sixth item of a list',
params: [{name: 'lst', type: 'list'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 6) {
throw new Error('Tried to get the 6th element of list with only ' + arr.length + ' elements');
}
return arr[5];
}
});
var seventh = $b({
name: 'seventh',
desc: 'Get the seventh item of a list',
params: [{name: 'lst', type: 'list'}],
fn: function(lst) {
var arr = listToArray(lst);
if (arr.length < 7) {
throw new Error('Tried to get the 7th element of list with only ' + arr.length + ' elements');
}
return arr[6];
}
});
// pulled out into its own function because we use it elsewhere
var _rest = function(x) {
if (x.length == 2 && x[1] != null) {
return x[1];
} else {
return x.slice(1);
}
};
var rest = $b({
name: 'rest',
alias: 'cdr',
desc: 'Get everything after the first item in a pair or list',
params: [{name: 'x', type: 'pair'}],
fn: _rest
});
var but_last = $b({
name: 'but_last',
alias: 'initial',
desc: 'Get everything except the last item in a list',
params: [{name: 'lst', type: 'list'}],
fn: function (lst) {
return arrayToList(_.initial(listToArray(lst)));
}
});
var last = $b({
name: 'last',
desc: 'Get the last item in a list',
params: [{name: 'lst', type: 'list'}],
fn: function (lst) {
return _.last(listToArray(lst));
}
});
var list_ref = $b({
name: 'list_ref',
desc: 'Get the nth item of a list (0-indexed)',
params: [{name: 'lst', type: 'list'},
{name: 'n', type: 'nat'}],
fn: function(lst, n) {
var array = listToArray(lst);
if (n >= array.length) {
throw new Error("Tried to the " + (n+1) + "th item in a list that only contains " + array.length + ' items');
} else {
return array[n];
}
}
});
var list_elt = $b({
name: 'list_elt',
desc: 'Get the nth item of a list (1-indexed)',
params: [{name: 'lst', type: 'list'},
{name: 'n', type: 'nat'}],
fn: function(lst, n) {
if (n < 1) {
throw new Error('The n argument to list-elt should be an integer >= 1');
}
return list_ref(lst, n-1);
}
});
var take = $b({
name: 'take',
desc: 'Get the first n items in a list. If there are fewer than n items in the list, returns just the list.',
params: [{name: 'lst', type: 'list'},
{name: 'n', type: 'nat'}],
fn: function(lst,n) {
return arrayToList(listToArray(lst).slice(0,n));
}
});
var drop = $b({
name: 'drop',
desc: 'Drop the first n items from a list. If there are fewer than n items in the list, return the empty list.',
params: [{name: 'lst', type: 'list'},
{name: 'n', type: 'nat'}],
fn: function(lst,n) {
return arrayToList(listToArray(lst).slice(n));
}
});
var sort = $b({
name: 'sort',
desc: 'Sort a list according to a comparator function cmp(a,b) that returns a number greater than 0 if a > b, 0 if a == b, and a number less than 0 if a < b',
params: [{name: "lst", type: "list"},
{name: "[cmp]", type: "function", default: ">"}],
fn: function(lst, cmp) {
var arr = listToArray(lst);
var sortedArr;
if (cmp === undefined ) {
sortedArr = arr.sort();
} else {
sortedArr = arr.sort( cmp );
}
return arrayToList( sortedArr, true );
}
});
var unique = $b({
name: 'unique',
desc: 'Get the unique items in a list',
params: [{name: "lst", type: "list"},
{name: "[eq]", type: "function", desc: "Optional equality comparison function", default: "equal?"}
],
fn: function(lst, eq) {
eq = eq || is_equal;
var arr = listToArray(lst);
var uniques = [];
for(var i = 0, ii = arr.length ; i < ii; i++) {
var v = arr[i];
var alreadySeen = false;
for(var j = 0, jj = uniques.length; j < jj; j++) {
if (eq(v, uniques[j])) {
alreadySeen = true;
break;
}
}
if (!alreadySeen) {
uniques.push(v);
}
}
return arrayToList(uniques, true);
}
});
var nub = $b({
name: 'nub',
desc: 'Remove duplicates with equality as ===',
params: [{name: "lst", type: "list"}],
fn: function(lst) {
return arrayToList(_.uniq(listToArray(lst)));
}
});
var list_index = $b({
name: 'list_index',
alias: 'position',
desc: '',
params: [{name: "lst", type: "list"},
{name: "x"}],
fn: function(lst, x) {
var arr = listToArray(lst);
return arr.indexOf(x);
}
});
var map_at = $b({
name: 'map_at',
alias: 'f-at',
desc: '',
params: [{name: "lst", type: "list"},
{name: "i", type: "nat"},
{name: "f", type: "function"}],
fn: function(lst, i, f) {
var arr = listToArray(lst);
arr[i] = f(arr[i]);
return arrayToList(arr, true);
}
});
var max = $b({
name: 'max',
desc: 'Maximum of arguments',
params: [{name: "[x ...]", type: "real", desc: ""}],
fn: function() {
// we don't use Math.max.apply here because that
// can choke on small-ish arguments sizes (~80k suffices)
// because v8 is weird with nested apply calls
var maxVal = -Infinity;
for(var i = 0, n = arguments.length; i < n; i++) {
if (arguments[i] > maxVal) {
maxVal = arguments[i];
}
}
return maxVal;
}
});
var min = $b({
name: 'min',
desc: 'Minimum of arguments',
params: [{name: "[x ...]", type: "real", desc: ""}],
fn: function() {
// we don't use Math.min.apply here because that
// can choke on small-ish arguments sizes (~80k suffices)
// because v8 is weird with nested apply calls