~jojo/Carth

ref: 55fb4f948f1f3797078b584dc60b4f7dd68b37ed Carth/std/math.carth -rw-r--r-- 909 bytes
55fb4f94JoJo Check `cast` in Infer instead of Gen 5 months ago
                                                                                
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
(import iter)

(define: pi F64 3.14159265358979323846264338327950288)
(define: inv-pi F64 0.318309886183790671537767526745028724)

(extern sin (Fun F64 F64))
(extern cos (Fun F64 F64))
(extern tan (Fun F64 F64))

(define (inc n) (+ n (cast 1)))
(define (dec n) (- n (cast 1)))

(define (neg x) (- 0 x))

(define (min a b) (if (< a b) a b))
(define (max a b) (if (> a b) a b))

(define (square x) (* x x))

(define (and p q) (if p q    False))
(define (or  p q) (if p True q))

(define (ands xs) (foldl and True xs))
(define (all f xs) (ands (map f xs)))

(define not (fmatch (case True False)
                    (case False True)))

(define (divisible? n m) (= (rem n m) (cast 0)))

(define (powi x y)
  (define (go acc x y)
    (if (= y 0)
        acc
      (if (divisible? y 2)
          (go acc (square x) (/ y 2))
        (go (* acc x) x (dec y)))))
  (go (cast 1) x y))

(define sum (foldl + (cast 0)))