绘图

Scheme] 
(define (plot l) (stree->tree l))
Scheme] 
(plot ‘(frac 1 2))
Scheme] 
(plot ‘(point 0 0))
Scheme] 
(plot ‘(graphics (point "-5" "-5") (point "5" "5"))) ; quasiquote ‘
Scheme] 
(plot ‘(graphics (point ,(number->string (+ 1 2)) ,(number->string (+ 1 3))) (point 0 0)))
Scheme] 
(plot ‘(graphics (point "0" "0") (point "1" "1") (line (point "0" "0") (point "1" "1")))))
Scheme] 
(plot ‘(cline (point 0 0) (point 3 0) (point 0 4)))
Scheme] 
(kbd-map ("d i a n var" (insert (utf8->cork "点"))))
Scheme] 
(kbd-map ("h t var" (insert (utf8->cork "绘图"))))
Scheme] 
(kbd-map ("t u var" (insert (utf8->cork "图"))))
Scheme] 
(kbd-map ("s j x var" (insert (utf8->cork "三角形"))))
Scheme] 
(kbd-map ("z s j x var" (insert (utf8->cork "正三角形"))))
Scheme] 
(define (scm2stm x)
  (define (x->string x)
    (cond ((number? x) (number->string x))
          ((list? x) (scm2stm x))
          (else x)))
  (if (eq? () x)
      ()
      (cons (x->string (car x))
            (scm2stm (cdr x))))))
Scheme] 
(define (绘图 x)
  (stree->tree (scm2stm x)))

绘图

Scheme] 
(define (点 x y) ‘(point ,x ,y))

Scheme] 
(define (图 . objects)
  (cond ((nlist? objects) '(graphics "" ""))
        ((== objects '()) '(graphics "" ""))
        (else ‘(graphics "" ,@objects))))

Scheme] 
(绘图 (图 (点 1 1) (点 2 2)))
Scheme] 
(define (triangle x y z)
  ‘(cline ,x ,y ,z))

triangle

Scheme] 
(define (三角形 x y z) (triangle x y z))

三角形

Scheme] 
(绘图 (三角形 (点 0 0 ) (点 0 3) (点 4 0)))

Scheme] 
(car (cdr (cdr (点 1 2)))) ; third

2

Scheme] 
(car (cdr (点 1 2))) ; second

1

Scheme] 
(list-ref (list 1 2 3 4 5) 1)

2

Scheme] 
(define (正三角形 A a)
  (三角形
       A
       (点 (+ a (second A))
           (third A))
       (点 (+ (* 0.5 a) (second A))
           (+ (third A) (* (sqrt 0.75) a))))))

正三角形

Scheme]
Scheme] 
(scm2stm (正三角形 (点 0 0) 3))

(cline (point "0" "0") (point "3" "0") (point "1.5" "2.598076211353316"))

Scheme] 
(绘图 (正三角形 (点 0 0) 3))

Scheme]

寻找质数

Scheme] 
(define (square x) (* x x))

square

Scheme] 
(define (smallest-divisor n)
  (find-divisor n 2))

smallest-divisor

Scheme] 
(define (find-divisor n test-divisor)
  (cond ((> (square test-divisor) n) n)
        ((divides? test-divisor n) test-divisor)
        (else (find-divisor n (+ test-divisor 1)))))

find-divisor

Scheme] 
(define (divides? a b)
  (= (remainder b a) 0))

divides?

Scheme] 
(define (prime? n)
  (= n (smallest-divisor n)))

prime?

Scheme] 
(prime? 7)

#t

Scheme] 
(smallest-divisor 7)

7

Scheme] 
(find-divisor 7 2)

7

Scheme]
Scheme] 
(define (square x) (* x x))

square

Scheme] 
(define (expmod base exp m)
  (cond ((= exp 0) 1)
        ((even? exp)
         (remainder (square (expmod base (/ exp 2) m))
                    m))
        (else
         (remainder (* base (expmod base (- exp 1) m))
                    m))))

expmod

Scheme] 
(define (fermat-test n)
  (define (try-it a)
    (= (expmod a n n) a))
  (try-it (+ 1 (random (- n 1)))))

fermat-test

Scheme] 
(define start 0)

0

Scheme]
(debug-message "std" "\nHello\n")
Scheme] 
(define (show-time msg ts)
  (debug-message "std"
    (string-append msg (number->string (- (texmacs-time) ts)) "\n")))

show-time

29 msec

Scheme] 
(define (fast-prime? n times ts)
  (cond ((= times 0)
         (begin
           (show-time "true time: " ts)
           #t))
        ((fermat-test n) (fast-prime? n (- times 1) ts))
        (else (begin
               (show-time "false time: " ts)
               #f))))

fast-prime?

28 msec

Scheme] 
(define (wrap-prime? n times)
  (fast-prime? n times (texmacs-time)))

wrap-prime?

28 msec

Scheme] 
(wrap-prime? 99999787 100000)

#t

1.456 sec

Scheme] 
(current-time)

1711437953

Scheme] 
(define start (current-time))

1711438362

Scheme] 
(define end (current-time))

1711438370

Scheme] 
(- end start)

8

Scheme] 
(texmacs-time)

10853179

Scheme] 
(texmacs-time)

10859400

Scheme]

墨者百科:https://gitee.com/XmacsLabs/tmmlwiki/raw/main/数学/费马小定理.tm