OLD | NEW |
| (Empty) |
1 part of lisp; | |
2 | |
3 /** | |
4 * The standard library. | |
5 */ | |
6 class Standard { | |
7 | |
8 /** Imports the standard library into the [environment]. */ | |
9 static Environment import(Environment environment) { | |
10 evalString(lispParser, environment, _standardLibrary); | |
11 return environment; | |
12 } | |
13 | |
14 /** A simple standard library, should be moved to external file. */ | |
15 static String _standardLibrary = """ | |
16 ; null functions | |
17 (define null '()) | |
18 (define (null? x) (= '() x)) | |
19 | |
20 ; booleans | |
21 (define true (and)) | |
22 (define false (or)) | |
23 | |
24 ; list functions | |
25 (define (length list) | |
26 (if (null? list) | |
27 0 | |
28 (+ 1 (length (cdr list))))) | |
29 | |
30 (define (append list1 list2) | |
31 (if (null? list1) | |
32 list2 | |
33 (cons (car list1) (append (cdr list1) list2)))) | |
34 | |
35 (define (list-head list index) | |
36 (if (= index 0) | |
37 (car list) | |
38 (list-head | |
39 (cdr list) | |
40 (- index 1)))) | |
41 | |
42 (define (list-tail list index) | |
43 (if (= index 0) | |
44 (cdr list) | |
45 (list-tail | |
46 (cdr list) | |
47 (- index 1)))) | |
48 | |
49 (define (for-each list proc) | |
50 (while (not (null? list)) | |
51 (proc (car list)) | |
52 (set! list (cdr list)))) | |
53 | |
54 (define (map list proc) | |
55 (if (null? list) | |
56 '() | |
57 (cons (proc (car list)) | |
58 (map (cdr list) proc)))) | |
59 | |
60 (define (inject list value proc) | |
61 (if (null? list) | |
62 value | |
63 (inject | |
64 (cdr list) | |
65 (proc value (car list)) | |
66 proc))) | |
67 """; | |
68 } | |
OLD | NEW |