-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprelude.haha
161 lines (129 loc) · 3.85 KB
/
prelude.haha
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
;;;;;;;;; Prelude is used as a way to add functionality to the language and programs as a library
;;; Arithmatic prims
(define-prim + 1 2 3)
(define-prim - 1 2 3)
(define-prim * 1 2 3)
(define-prim / 1 2 3)
(define-prim = 1 2 3)
(define-prim > 1 2 3)
(define-prim < 1 2 3)
(define-prim <= 1 2 3)
(define-prim >= 1 2 3)
; ; ;;; prim-ops
(define-prim modulo 2)
(define-prim null? 1)
(define-prim equal? 2)
(define-prim eq? 2)
(define-prim cons 2)
(define-prim car 1)
(define-prim cdr 1)
; (define-prim float->int 1)
; (define-prim int->float 1)
; ;;; hash prims
; (define-prim hash)
; (define-prim hash-ref 2)
; (define-prim hash-set 3)
; (define-prim hash-keys 1)
; (define-prim hash-has-key? 2)
; (define-prim hash-count 1)
; ;;; set prims
; (define-prim set)
; (define-prim set->list 1)
; (define-prim list->set 1)
; (define-prim set-add 2)
; (define-prim set-member? 2)
; (define-prim set-remove 2)
; (define-prim set-count 1)
; ;;; string prims
; (define-prim string? 1)
; (define-prim string-length 1)
; (define-prim string-ref 2)
; (define-prim substring 3)
; (define-prim string-append 2)
; (define-prim string->list 1)
; ;;; other new prims
; (define-prim exact-floor 1)
; (define-prim exact-ceiling 1)
; (define-prim exact-round 1)
; (define-prim abs 1)
; (define-prim max 1)
; (define-prim min 1)
; (define-prim expt 2)
; (define-prim sqrt 1)
(define-prim remainder 2)
; (define-prim quotient 2)
; (define-prim random 1 2)
; (define-prim symbol? 1)
(define-prim pair? 1)
; (define-prim positive? 1)
; (define-prim negative? 1)
(define-prim list 1 2 3 4)
; ;;; other predicates and HOFs
; ; (define (list . x) x)
(define (even? x)
(equal? 0 (modulo x 2)))
; (define (boolean? x)
; (if (or (eq? x '#t) (eq? x '#f)) #t #f))
(define (odd? x)
(equal? 1 (modulo x 2)))
(define (list-ref lst n)
(if (= 0 n)
(car lst)
(list-ref (cdr lst) (- n 1))))
(define (member item lst)
(cond
[(or (null? item) (null? lst)) #f]
[(equal? item (car lst)) lst]
[else (member item (cdr lst))]))
(define (member? x lst)
(if (null? lst) #f (if (equal? (car lst) x) #t (member? x (cdr lst)))))
(define (length lst)
(if (null? lst)
0
(+ 1 (length (cdr lst)))))
(define (map proc lst)
(if (null? lst) (list) (cons (proc (car lst)) (map proc (cdr lst)))))
(define (filter op lst)
(if (null? lst)
(list)
(if (op (car lst))
(cons (car lst) (filter op (cdr lst)))
(filter op (cdr lst)))))
; (define (drop lst n)
; (if (= n 0)
; lst
; (drop (cdr lst) (- n 1))))
(define (foldl fun acc lst)
(if (null? lst) acc (foldl fun (fun (car lst) acc) (cdr lst))))
(define (foldr fun acc lst)
(if (null? lst) acc (fun (car lst) (foldr fun acc (cdr lst)))))
; (define (reverse-helper lst lst2)
; (if (null? lst) lst2 (reverse-helper (cdr lst) (cons (car lst) lst2))))
; (define (reverse lst)
; (reverse-helper lst (list)))
(define (append1 lhs rhs)
(if (null? lhs)
rhs
(cons (car lhs)
(append1 (cdr lhs) rhs))))
(define (append l1 l2 . lsts)
(if (null? lsts)
(append1 l1 l2)
(foldr append1 (list) (append1 (list l1 l2) lsts))))
; (define (take-helper lst n lst2)
; (if (= n 0)
; (reverse lst2)
; (take-helper (cdr lst) (- n 1) (cons (car lst) lst2))))
; (define (take lst n)
; (take-helper lst n (list)))
(define (list-set lst index value)
(if (= index 0)
(cons value (cdr lst))
(cons (car lst)
(list-set (cdr lst) (- index 1) value))))
(define (flatten lst)
(cond [(null? lst) (list)]
[(pair? lst)
(append (flatten (car lst)) (flatten (cdr lst)))]
[else (list lst)]))
; ; ;;; ormap, andmap, both requires length to work properly. once done will impelement them.